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

Re: Bless and Do

by kennethk (Monsignor)
on Jun 17, 2014 at 16:08 UTC ( #1090165=note: print w/ replies, xml ) Need Help??


in reply to Bless and Do

The do block defines the logical context of the my declaration. Essentially, we:

  1. define $x
  2. assign the value 49 to it
  3. take a reference
  4. let $x go out of scope
  5. bless the reference
  6. store the reference

Since Perl does not have the concept of an anonymous scalar ref (in contrast to {} and []), your kludge is one way of getting that. The best reason to use it is because it works. The worst reason is that you had to ask the question; it doesn't scan unless you know the trick. Were I to write it, I'd probably go with the more verbose:

my $variable; ANON_SCALAR_BLOCK: { my $x = 49; $variable = bless \$x, 'Rates::Notification'; }
If you really want to use a do, you could also
my $variable = do { my $x = 49; bless \$x, 'Rates::Notification'; };
which I think scans better.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


Comment on Re: Bless and Do
Select or Download Code
Re^2: Bless and Do
by Anonymous Monk on Jun 17, 2014 at 20:20 UTC

    An interesting thing to note: Data::Dumper uses a form very similar to OP with blessed references.

    perl -MData::Dumper -e 'print Dumper bless \((49)[0]), "Rat::Notificat +ion";' $VAR1 = bless( do{\(my $o = 49)}, 'Rat::Notification' );

Re^2: Bless and Do on the outside
by Anonymous Monk on Jun 18, 2014 at 00:00 UTC

    If you really want to use a do, you could also

    my $variable = do { my $x = 49; bless \$x, 'Rates::Notification'; };
    which I think scans better.

    Yes, do on the outside always scans better, its like creating a sub and calling it immediately,  (sub { ... })->(); except prettier :)

      > Yes, do on the outside always scans better, its like creating a sub and calling it immediately, (sub { ... })->(); except prettier :)

      To avoid misunderstandings: It's similar but not identical!

      DB<106> sub tst { (sub { return 5 })->(); return 10 } DB<107> tst() => 10 DB<108> sub tst { do { return 5 }; return 10 } DB<109> tst() => 5

      Like with map and grep is the block following do not an anonymous sub!

      Cheers Rolf

      (addicted to the Perl Programming Language)

        To avoid misunderstandings: It's similar but not identical!

        "Like" means "similar" I think dictionary can agree!

        Like with map and grep is the block following do is not an anonymous sub!

        Of course we're discussing looks!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1090165]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2014-12-26 01:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (163 votes), past polls