Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Return values from package methods

by rob_au (Abbot)
on Aug 19, 2003 at 04:02 UTC ( [id://284787]=perlquestion: print w/replies, xml ) Need Help??

rob_au has asked for the wisdom of the Perl Monks concerning the following question:

I have come across an oddity while performing some development work with Perl 5.6.1 which I cannot explain - The oddity is highlighted by the following segment of code:

#!/usr/bin/perl use Data::Dumper; my $test = Test->new; print Dumper( $test->test ); exit 0; package Test; use strict; sub new { return bless {}, shift; } sub test { } 1; __END__

Under 5.8.0 this script outputs nothing (as I would expect) - However under 5.6.1, the call to the package method test results in the return of the class object. For example:

bluebottle:~# perl test.pl $VAR1 = bless( {}, 'Test' );

I have not had a chance as yet to test this with Activestate builds or older versions of Perl. Whilst this isn't a real issue holding up my development work, it is something which I am at a loss to explain. Can anyone enlighten me on this one?

 

perl -le 'print+unpack"N",pack"B32","00000000000000000000001001111010"'

Replies are listed 'Best First'.
Re: Return values from package methods
by esh (Pilgrim) on Aug 19, 2003 at 04:50 UTC

    I'm another lost soul on 5.6.1 until 5.8.1 comes out to solve the UTF problems in 5.8.0, so I verified the test case and removed some of the unnecessary complexity to get to the essense.

    It turns out that the behavior you're seeing is not related to the class system. Apparently the empty function is returning its parameters, but only if it is called in list context. It returns undef if called in scalar context.

    #!/usr/bin/perl -w use strict; use Data::Dumper; my $x = test('a', 'b'); my @y = test('a', 'b'); print Dumper( $x, \@y ); sub test {}
    returns
    $VAR1 = undef; $VAR2 = [ 'a', 'b' ];

    Update: s/UTC/UTF/

    -- Eric Hammond

      I'm another lost soul on 5.6.1 until 5.8.1 comes out to solve the UTC problems in 5.8.0

      Are the "UTC problems in 5.8.0" really so bad? For sure, 5.8.1 will fix bunch of things, but these are a fairly small and abstruse set, compared to the set of things that 5.8.0 does right and 5.6.1 doesn't do at all (or does wrong).

        (OFF TOPIC)

        Unfortunately, our users kept hitting a UTF-8 problem on our web site which made it unusable (even though we are not using UTF anywhere in our code). Everything works smoothly on 5.6.1.

        We tried very hard with help from the community to solve the problems or find workarounds, but were eventually told that this specific problem would be fixed in the next release.

        We wait patiently and eagerly.

        Update: s/UTC/UTF/ per liz.

        -- Eric Hammond

Re: Return values from package methods
by MarkM (Curate) on Aug 19, 2003 at 04:34 UTC

    Sounds like a bug that was fixed.

    Magic is needed to implement the 'subroutines exitted using a means other than die() or return(), result with the last statement executed, in the context that the subroutine was called within.' Perhaps in 5.6, this magic was a little more lazy, and never explicitly set the return result to be an empty list in the case that a subroutine has no statement in it?

    Perhaps it is unreasonable to expect a reliable result for a subroutine that does not implicitly or explicitly return a result? :-)

    Cheers,
    mark

Re: Return values from package methods
by wirrwarr (Monk) on Aug 19, 2003 at 13:04 UTC
    I tried
    #!/usr/bin/perl use Data::Dumper; my $test = Test->new; my $a = $test->test; my $b = \$test->test; print Dumper( $test->test ); print Dumper( $a ); print Dumper( $b ); exit 0; package Test; use strict; sub new { return bless {}, shift; } sub test { } 1; __END__
    That gives me on 5.6.1
    $VAR1 = bless( {}, 'Test' ); $VAR1 = undef; $VAR1 = \bless( {}, 'Test' );
    Looks rather strange to me.
      In 5.6.0 on AIX, I did:
      #!/usr/bin/perl use Data::Dumper; my $test = Test->new; $test->xyz; my $a = $test->test(1); my $b = \$test->test(2); print Dumper( $test->test(3) ); print Dumper( $a ); print Dumper( $b ); exit 0; package Test; use strict; sub new { return bless {}, shift; } sub xyz { (1..3) } sub test { } 1; __END__ ----- $VAR1 = bless( {}, 'Test' ); $VAR2 = 3; $VAR1 = undef; $VAR1 = \2;

      It looks like the implicit return in list context, barring no other statements, is @_. Having an intervening statement didn't do anything.

      ------
      We are the carpenters and bricklayers of the Information Age.

      The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Return values from package methods
by SavannahLion (Pilgrim) on Aug 19, 2003 at 16:50 UTC
    (Off Topic)

    Sorry to go off topic I'm really new to Perl and I haven't quite figured out where the Private Message pages are yet....

    But what does your signature:

    perl -le 'print+unpack"N",pack"B32","00000000000000000000001001111010"'

    do?

    I followed the code through but I'm getting lost at the +unpack pack bits.

    Thanks for you patience.

      My signature is simply prints the number of the post as per the count of number of write-ups on my home node (and as such my signature can be seen to change between posts). Whilst I could have implemented this simply with something like perl -le 'print 636', I decided to make it a little more interesting.

      The pack function takes a list of values and converts these to the internal machine-level representation of the values as per the first argument to this function known as the "template". In my signature, this is used to convert a string with a 32-bit binary number into a machine-level representation of this value. The reverse of this is performed with the unpack function which converts from an internal machine-level representation to a string based again upon the template argument to the function. In the case of my signature, this outputs a big-endian order unsigned long integer representing the post number.

       

      perl -le 'print+unpack"N",pack"B32","00000000000000000000001001111100"'

        I guess something must be flaky with my installation of Perl (5.8.0 Win) on this machine. When I do a Copy & Paste into the command line, it doesn't do anything. goes through without any errors. Turning on the w flag just blurts out

        Useless use of a constant in void context at -e line 1.

        So I create a script file and edited the line you have a bit and I get back an answer of 636.

        So I guess I must have something misconfigured or forgot something or something :(

        Oh well....

        Oh yeah, I guess I better give you your hijacked thread back.

        Thanks for you patience.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://284787]
Approved by Zaxo
Front-paged by gmax
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found