in reply to What is correct way to reference?

Hi, see what the Monks said about using the right tools to work with your XML.

But in general, one way to do what you want when you don't know whether you have a simple scalar value or a reference to an array, without a loop or multiple print statements, is to use the default list separator, stored in the special global var $", and just interpolate the list, after you extract it based on your ref check:

perl -Mstrict -wE ' my $foo = "bar"; my $baz = [ "qux", "quux" ]; # we have both kinds local $" = "\n"; # change list separator to a newline say ( ref $_ eq "ARRAY" ? "@$_" : $_ ) for ( $foo, $baz ); '
Output:
bar qux quux

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: What is correct way to reference?
by AnomalousMonk (Archbishop) on May 22, 2017 at 20:48 UTC
        local $" = "\n";             # change list separator to a newline

        say ( ref $_ eq "ARRAY" ? "@$_" : $_ ) for ( $foo, $baz );

    Wouldn't it be easier/quicker/clearer/more informative to use something like Data::Dumper::Dumper() (which is core) or Data::Dump::dd() (not core, but I like it a lot) to dump a scalar or reference and see exactly what its value or structure is? Why go through such specialized acrobatics?


    Give a man a fish:  <%-{-{-{-<

      Yes, of course! This is a completely contrived exercise to show $" ... simply for edification.

      The way forward always starts with a minimal test.