perl - Using my() changes XML::Bare behavior drastically -


When I run this Perl script:

  #! / Bin / perl XML:: bare; $ Ob = new XML :: berre (text = & gt; 'lt; xml & gt; name & gt; bob & lt; / name & gt; & lt; / xml & gt;'); For $ I (key% {$ ob-> {xml}}) {print "key: $ i \ n";}   

I did not get any output. However, If I put $ ob inside my () :

  then #! / Bin / perl using XML :: Bare; Mine ($ Ob) = new XML :: berre (text => gt; 'gt; & lt; xml & gt; & lt; name & gt; Bob & lt; / name & gt; & lt; / xml & gt; ); For $ I (key% {$ ob-> {xml}}) {print "key: $ i \ n";}   

I get this output:

  key: _z key: _i key: xml key: _pos   

why my () changes this behavior so intensely , Especially at the top level, where should there be no effect of my () ?

First of all, you should always start your Perl scripts

 < Code> use strict; Use warnings;   

This will inspire you to declare all your variables with my , which captures many typo and simple mistakes.

In your case, it is not really the cause of behavior in my , but the brackets, which enter the $ ob list reference.

XML :: bread, we find it in the manufacturer:

  sub new {... bless $ self, $ square; $ Refund if (merging!); Return ($ self, $ self-> pars ()); }   

Note that the second back line call -> on the new object is for you. Your first instance of parse, which you forgot to do, is the reason why there was no data in it.

Then you can say

  my $ obj = XML :: Bare-> New (Text = & gt; & lt; XML & gt; & lt; name & gt; Bob & lt; / name & gt; & lt; / XML & gt; ';); # Scalar in context $ obj-> Pars;   

or

  my $ (obj) = XML :: Bare-> new (text = & gt; '& lt; xml & gt; & quot; Lt; name & gt; Bob & lt; / name & gt; & lt; / XML & gt; '); # In the reference reference   

and they should be identical.

This is a very weird interface option, but I'm not familiar with XML :: Bare .

Also keep in mind that I have left the indirect method syntax ( XML :: Bare-> instead of new code: new XML :: Bare ). This helps in avoiding something.

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -