Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Manually adding delimiters instead of join()

by Anonymous Monk
on Oct 12, 2004 at 17:08 UTC ( #398604=perlquestion: print w/replies, xml ) Need Help??

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

Why must you join() these values instead of just storing them with common separators?
This stores only the first form field/variable and forgets everything else. But if you join the variables, it works.
my $data = join("::", $password, $name, $email, $website, $start); $users{$username} = $data;
Can someone explain why you can't manually add your own delimiters in this particular line?

Replies are listed 'Best First'.
Re: Manually adding delimiters instead of join()
by borisz (Canon) on Oct 12, 2004 at 17:23 UTC
    Thats becourse perl tries to expand a var in another namespace as requested with ::. Try this instead or use another seperator:
    $data = "${password}::${name}::${email}::${website}::${start}";
Re: Manually adding delimiters instead of join()
by kvale (Monsignor) on Oct 12, 2004 at 17:26 UTC
    The answer is that your particular delimiter '::' has syntactic significance in Perl -- it is the package delimiter. Thus the first form is parsed as a (rather gnarly) package variable. But the second does what you want: substitute each variable individually and then join with the syntax sensitive ::.

    If your delimiter had been something more benign, like whitespace, both forms would work.


Re: Manually adding delimiters instead of join()
by dragonchild (Archbishop) on Oct 12, 2004 at 17:37 UTC
    Because :: is a special delimiter in Perl. If you tried ;; or || or anything else, for that matter, I suspect it would it would work.

    Also, turn strict on. :-)

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Manually adding delimiters instead of join()
by dave_the_m (Monsignor) on Oct 12, 2004 at 17:30 UTC
    The double colon can be part of a variable name, as in $foo::bar. Try putting parentheses around the variable name, eg


Re: Manually adding delimiters instead of join()
by gawatkins (Monsignor) on Oct 12, 2004 at 20:32 UTC

    If you are set on using :: as your separator, you would have to escape them like any other special characters. You could do something like this:

    $users{$username}"$password\:\:$name\:\:$email\:\:$website\:\:$start"; Thanks
    Greg W
      Alternately, since you only need to break up the :: variable interpolation
      $users{$username} = "$password\::$name\::$email\::$website\::$start";
      or even (although this isn't any better than join-not that there is anything wrong with join)

      use strict; use warnings; use diagnostics;

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://398604]
Approved by kvale
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2022-10-04 07:22 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (16 votes). Check out past polls.