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

Monks-
When I run this code:
my $x = '2D6-324'; print (split('-',$x))," complete\n";
it doesn't print "complete" and the carriage return. Why is that? I've noticed that if I remove the parens enclosing the split, it works.

Replies are listed 'Best First'.
Re: split question
by davorg (Chancellor) on Oct 09, 2002 at 16:51 UTC

    You're not running with warnings turned on are you? If you had been you would have got the message

    print (...) interpreted as function at ./foo.pl line x. Useless use of a constant in void context at ./foo.pl line x.

    Which pretty much explains what's going on.

    You should always ask Perl for as much help as possible when tracking down bugs.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Well put!
Re: split question
by zigdon (Deacon) on Oct 09, 2002 at 16:21 UTC

    I think it's because this is getting parsed as:

    my $x = '2D6-324'; print(split('-',$x)) ," complete\n";

    so you're printing the split, and the "executing" the text. If you turned on warnings, you'd see "Useless use of a constant in void context". Either add another () enclosing all you want to print, or get rid of the () around split.

    -- Dan

Re: split question
by broquaint (Abbot) on Oct 09, 2002 at 16:22 UTC
    Let's see that again, slightly reformatted
    my $x = '2D6-324'; print(split '-',$x)," complete\n";
    Well you can see what's going on there - the print is binding to the initial set of parentheses. Instead you probably want something like this
    my $x = '2D6-324'; print split('-', $x)," complete\n"; __output__ 2D6324 complete

    HTH

    _________
    broquaint

Re: split question
by jjdraco (Scribe) on Oct 09, 2002 at 16:24 UTC
    because the outer set of () are for the print function, so the complete newline is out side the print statemant. if you use strict and warnings at the beginning of your code you should get an error message stating useless use of constant, and that would be refering to the  ," complete\n";

    jjdraco
    learning Perl one statement at a time.