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

Hi Monks,

I am trying to reverse the string. So I tried the following

 perl -e "print reverse ("Monks");"

But, It doesn't reverse print.

OUTPUT:

Monks

But, When I use scalar It print reverse

perl -e "print scalar reverse ("Monks");"

OUTPUT:

sknoM

I need to know, why its not printing when i use reverse without scalar.

Thanks in Advance
Ashok

Replies are listed 'Best First'.
Re: Reverse the String
by Fletch (Bishop) on Nov 06, 2006 at 14:43 UTC

    Because reverse in list context reverses the order of the items in the list; in scalar context it reverses the characters in a string. Just like the documentation says it does.

    Update: And your confusion may be from the fact that print is providing a list context to reverse which is triggering its reverse-a-LIST behavior even though it's only been given a single element list.

Re: Reverse the String
by jbert (Priest) on Nov 06, 2006 at 15:03 UTC
    One of the more interesting (i.e. both useful and potentially confusing) aspects of perl is that a function can determine whether it's caller wants an array or a scalar, via the 'wantarray' function.

    This is called being called "in scalar context" or "in list context".

    This allows the same function to do two different useful jobs, but can be surprising to someone who expects the following snippets to do the same thing (which they might not):

    # ------------------------------ # Call in array context # wantarray() will return true inside some_function my @ary = some_function; # And see how many items were returned. my $num_items = scalar @ary; # ------------------------------ # Call with explicit scalar context: # wantarray() will return false inside some_function my $num_items = scalar some_function; # $num_items might or might not be the same as the array # context call above, depending on whether some_function # chooses to check want_array and do something different. # ------------------------------ # Call with explicit scalar context: # wantarray() will return false inside some_function my $num_items = some_function; # As above.
    There is some more on this in the 'Context' section here: perldata
Re: Reverse the String
by swampyankee (Parson) on Nov 06, 2006 at 18:12 UTC

    Reading the docs for reverse, one sees that it will reverse a string in scalar context (

    $doog = reverse('good')

    results in $doog being set to 'doog'), but it will reverse the order of the elements of a list in list context (

    @doog = reverse('good');

    results in @doog = ('good'), as a 1 element list looks the same when reversed (Perl may even optimize the reverse away in this case).

    Since print expects a list (print ($a, $b, $c) is perfectly valid Perl), Perl's behavior for

    print reverse('good');

    is the same as it is for

    @doog = reverse('good');

    emc

    At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

    —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.
Re: Reverse the String
by jort (Initiate) on Nov 06, 2006 at 22:23 UTC
    Since print expects an array, reverse assumes you want to reverse an array, consisting of one value. This is so you can print reverse('M','o','n','k','s');

    The solution is to convince reverse you want a scalar, not an array. There are many ways to do this:

    # 1. Assign it to a scalar: $a=reverse("monks"); # 2. Use the keyword "scalar": print scalar reverse("monks"); # 3. Use some kind of scalar-based manipulation print reverse("monks")."\n"; # 3a: do almost the same: print reverse("monks").""; # 3b: (reverses 100 to 001, then adds 1, printing "2". print reverse(100)+1;
A reply falls below the community's threshold of quality. You may see it by logging in.