Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
The attribute specification :default('') didn't work as expected . Here is my program.
#!/usr/bin/perl -w package Foo; { use Class::Std; my %a : ATTR( :default('') :get<a>); }; my $f = Foo->new(); print ref($f), "\n"; if (defined($f->get_a())) { print "yes\n"; } else { print "no\n"; } if ($f->get_a() eq '') { print "yes\n"; } else { print "no\n"; } package Bar; { use Class::Std; my %a : ATTR( :default('''') :get<a>); }; my $g = Bar->new(); print ref($g), "\n"; if (defined($g->get_a())) { print "yes\n"; } else { print "no\n"; } if ($g->get_a() eq '') { print "yes\n"; } else { print "no\n"; } print "Hello\n"; if (undef eq '') { print "yes\n" } else { print "no\n"; }
When executed, this perl script gives the following output.
./test14.pl Foo no Use of uninitialized value in string eq at ./test14.pl line 14. yes Bar yes yes Hello Use of uninitialized value in string eq at ./test14.pl line 31. yes
Based on the explanation of Class::Std in CPAN, I expected $f->get_a() to be the null string. However, according to the 2nd and 3rd line of output, it is actually undefined.

I have a conjecture. If you look at the source, you see that the given default value is first evaluated, after the string delimiters are removed. in the Foo case, eval '' returns normally with the undefined value. To initialize the hash bucket to the null string in Goo, I had to do :default(''''). Is this a bug?


In reply to Class::Std :default semantics by esharris

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-19 05:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found