Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Strange Observation [SOLVED]

by karlgoethebier (Abbot)
on Jul 10, 2015 at 13:13 UTC ( [id://1134161]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,

i wanted to write a TMTOWTDI in reply to Merge 2 strings like a zip and wrote this:

use warnings; use strict; use feature qw(say); # use diagnostics; # say zip ('ABCDEFGHIJ','abcde'); my $zipped= zip ('ABCDEFGHIJ','abcde'); say $zipped; sub zip { # join "", sort {qq(\L$a) cmp qq(\L$b)} split "", $_[0].$_[1]; join "", sort { lc $a cmp lc $b} split "", $_[0].$_[1]; } __END__

This works, but with say zip ('ABCDEFGHIJ','abcde'); i get:

monks>zip.pl Unquoted string "zip" may clash with future reserved word at \monks\zi +p.pl line 6. say() on unopened filehandle zip at monks\zip.pl line 6.

And with use diagnostics; i get:

say() on unopened filehandle zip at monks\zip.pl line 6 (#1) (W unopened) An I/O operation was attempted on a filehandle that w +as never initialized. You need to do an open(), a sysopen(), or a so +cket() call, or call a constructor from the FileHandle package.

Edit: Shortened output and fixed minor typos.

What is going on here?

Update: Ouch! I didn't see the space (and perltidy didn't remove it). Thank you very much to all that helped.

Thank you very much for any hint and best regards, Karl

«The Crux of the Biscuit is the Apostrophe»

Replies are listed 'Best First'.
Re: Strange Observation
by Athanasius (Archbishop) on Jul 10, 2015 at 13:53 UTC

    Hello karlgoethebier,

    The problem is that Perl doesn’t recognise zip as a subroutine when it parses the line in question. As toolic says, one way to solve this is to make the subroutine call unambiguous by removing the whitespace between the subroutine name and the opening parenthesis of the argument list.

    Another way is to provide a forward declaration:

    use warnings; use strict; use feature qw(say); sub zip; say zip 'ABCDEFGHIJ', 'abcde'; sub zip { join "", sort { lc $a cmp lc $b} split "", $_[0] . $_[1]; }

    Output:

    23:44 >perl 1303_SoPW.pl AaBbCcDdEeFGHIJ 23:45 >

    This is documented in the Camel Book (4th Edition, 2012, p. 960):

    • If you have just a NAME and no BLOCK, it’s a predeclaration of that name.... Named declarations are useful because the parser treats a name specially if it knows it’s a user-defined subroutine.... These are sometimes called forward declarations.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Strange Observation
by toolic (Bishop) on Jul 10, 2015 at 13:33 UTC
    Other observations...

    Tip #6 from the Basic debugging checklist: B::Deparse. The line parses as:

    say zip 'ABCDEFGHIJ', 'abcde';

    If I remove the space between zip and the paren:

    say zip('ABCDEFGHIJ','abcde');

    the warning goes away, I get the expected output, and it parses differently:

    say zip('ABCDEFGHIJ', 'abcde');
Re: Strange Observation
by Anonymous Monk on Jul 10, 2015 at 13:36 UTC
    Indirect object syntax strikes again...
    $ perl -E 'say STDOUT ("one", "two", "three")' onetwothree $ perl -E 'say STDOUT("one", "two", "three")' Undefined subroutine &main::STDOUT called at -e line 1.
    Note the presense and absence of a space after STDOUT.
Re: Strange Observation
by KurtSchwind (Chaplain) on Jul 10, 2015 at 13:34 UTC

    EDIT: I am an idiot. It's the space before the first paren. Please ignore my previous post. I haven't had my caffeine yet.

    Previous jibberish redacted

    --
    “For the Present is the point at which time touches eternity.” - CS Lewis
Re: Strange Observation [SOLVED]
by anonymized user 468275 (Curate) on Jul 14, 2015 at 09:48 UTC
    My reaction would be to just go ahead and give it the filehandle it wants instead of playing with the syntax options, e.g.
    say STDOUT zip ('ABCDEFGHIJ','abcde');
    Update: i.e. having done so, a space or not after 'zip' shouldn't be an issue

    One world, one people

      "...give it the filehandle...playing with the syntax..."

      Yes sure, but please compare say STDOUT zip ('ABCDEFGHIJ','abcde'); with say zip 'ABCDEFGHIJ', 'abcde'; and then tell me honestly who plays ;-) BTW, playing with the syntax isn't a bad thing.

      Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

        In this case I can promise I tested before posting.

        One world, one people

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-26 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found