On Dec 20, 2007 3:53 AM, Sayed, Irfan (Irfan) <[EMAIL PROTECTED]> wrote:
> Hi All,
>
> I have one string like this "test@/vobs/pvob_aic";
>
> Now I want only "test" from this string, so I wrote reg. ex. like this
>
> $test="test@/vobs/pvob_aic";
> $ts = ($test =~ m{(.+)@$});
> print "$ts\n";
>
> But I am getting output as 1 not a string "test"
snip

This is why you need to use the warnings pragma (and the strict
pragma).  If you had you would have seen this warning:

Possible unintended interpolation of @$ in string at f.pl line 7.

You have three:
1. @$ in the regex is being treated like the array @$.
2. You are using the regex in scalar context* instead of list context**.
3. The $ metacharacter will cause the pattern not to match (because
the @ symbol is not at the end of the line).

You can fix 1. with by escaping @.  You can fix 2. by using
parenthesis around the variables you are trying to assign to.  You can
fix 3. by either extending the regex or removing the $.

my ($ts) = $test =~ /(.+)[EMAIL PROTECTED]/;

or

my ($ts) = $test =~ /(.+)\@/;

* in list context, a regex returns the captures
** in scalar context, a regex returns a boolean value that says
whether or not it matched

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to