[25914] in Perl-Users-Digest
Perl-Users Digest, Issue: 8139 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jun 1 14:05:31 2005
Date: Wed, 1 Jun 2005 11:05:10 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 1 Jun 2005 Volume: 10 Number: 8139
Today's topics:
confused constructing a regex <uk.ac.ox.physics.teaching@leeg.invalid>
Re: confused constructing a regex <daveandniki@ntlworld.com>
Re: confused constructing a regex <pilkowsk@informatik.uni-marburg.de>
Re: confused constructing a regex (Anno Siegel)
Re: confused constructing a regex <tadmc@augustmail.com>
Encoding problem with automation of Word by Perl <daveandniki@ntlworld.com>
Re: Encoding problem with automation of Word by Perl <daveandniki@ntlworld.com>
Escaping blog patterns <graffiti@yahoo.com>
Re: Escaping blog patterns <tadmc@augustmail.com>
Re: lexical analyzer generators (Jay G. Scott)
Neat way of setting default values <graffiti@yahoo.com>
Re: Neat way of setting default values <mark.clementsREMOVETHIS@wanadoo.fr>
Re: Neat way of setting default values <noreply@gunnar.cc>
Re: Neat way of setting default values <graffiti@yahoo.com>
Re: Neat way of setting default values <noreply@gunnar.cc>
Re: Neat way of setting default values <xennar@yahoo.com>
Re: Neat way of setting default values <tadmc@augustmail.com>
Re: Neat way of setting default values <noreply@gunnar.cc>
Re: Neat way of setting default values[SOLVED] <graffiti@yahoo.com>
Re: Neat way of setting default values[SOLVED] <nobull@mail.com>
Re: Parsing Tracklisting - discussion <klubbheads_NO_SPAM@rogers.com>
Re: perl style: can I combine two steps into one? <cwilbur@chromatico.net>
Suppression of error messages if a regex does not match (David Joseph Bonnici)
Re: Suppression of error messages if a regex does not m <noreply@gunnar.cc>
Re: Suppression of error messages if a regex does not m <mark.clementsREMOVETHIS@wanadoo.fr>
Re: Suppression of error messages if a regex does not m <flavell@ph.gla.ac.uk>
Re: writing to file <hdotvdotniekerkathccnetdotnl>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 01 Jun 2005 15:13:01 +0100
From: leeg <uk.ac.ox.physics.teaching@leeg.invalid>
Subject: confused constructing a regex
Message-Id: <d7kfpe$422$1@news.ox.ac.uk>
I have an input file of a format that looks something like this:
{
foo = (
{
bar = "baz";
wibble = WOBBLE;
},
{
bar = "barney";
wibble = JELLY;
}
);
someKey = someValue;
someArray = (value1, value2);
blankDict = {};
};
I've noticed (and at the time was fairly proud of said epiphany) that
this is almost a declaration of an anonymous hash and with a little
tweaking I could eval it as such. However, I need to quote it properly,
and despite a number of attempts can't construct a regex that will do it.
I want to search for a list of characters which are not the various
formatting characters [^\(\){};,=] *and* are not already surrounded by
quotes, and then surround them by quotes.
I thought of:
$line =~ s/[\s\(\){};,=]+([^"\(\){};,=])+[\s\(\){};,=]+/"$1"/g;
but this converts the above into:
{
foo" "
{
bar = "baz";
wibble"E"
},
{
bar = "barney";
wibble"Y"
}
);
someKey"e"
someArray"1"value2);
blankDict" "
}
so isn't what I want. What I especially can't determine is why " =
someValue;" for instance would be replaced by "e". Could someone offer
some assistance?
Ta,
leeg.
------------------------------
Date: Wed, 01 Jun 2005 14:40:17 GMT
From: "Dave" <daveandniki@ntlworld.com>
Subject: Re: confused constructing a regex
Message-Id: <lNjne.4507$%21.193@newsfe2-gui.ntli.net>
"leeg" <uk.ac.ox.physics.teaching@leeg.invalid> wrote in message
news:d7kfpe$422$1@news.ox.ac.uk...
>I have an input file of a format that looks something like this:
>
> {
> foo = (
> {
> bar = "baz";
> wibble = WOBBLE;
> },
> {
> bar = "barney";
> wibble = JELLY;
> }
> );
> someKey = someValue;
> someArray = (value1, value2);
> blankDict = {};
> };
>
> I've noticed (and at the time was fairly proud of said epiphany) that this
> is almost a declaration of an anonymous hash and with a little tweaking I
> could eval it as such. However, I need to quote it properly, and despite
> a number of attempts can't construct a regex that will do it.
> I want to search for a list of characters which are not the various
> formatting characters [^\(\){};,=] *and* are not already surrounded by
> quotes, and then surround them by quotes.
>
> I thought of:
> $line =~ s/[\s\(\){};,=]+([^"\(\){};,=])+[\s\(\){};,=]+/"$1"/g;
> but this converts the above into:
> {
> foo" "
> {
> bar = "baz";
> wibble"E"
> },
> {
> bar = "barney";
> wibble"Y"
> }
> );
> someKey"e"
> someArray"1"value2);
> blankDict" "
> }
>
> so isn't what I want. What I especially can't determine is why " =
> someValue;" for instance would be replaced by "e". Could someone offer
> some assistance?
>
> Ta,
>
> leeg.
Your regex removes the "formatting characters" before and after the non
formatting characters block. Use lookahead/behind or capture them and put
them in. Also you are only capturing one character of the string you are
trying to quote, capture the whole string. i.e.:
$line =~ s/([\s\(\){};,=]+)([^"\(\){};,=]+)([\s\(\){};,=]+)/$1"$2"$3/g;
I'm not saying this will do what you want as I haven't looked into it in
detail, but it is clear that your original regex is deleting info that you
want to keep.
Dave
------------------------------
Date: Wed, 1 Jun 2005 16:46:32 +0200
From: Fabian Pilkowski <pilkowsk@informatik.uni-marburg.de>
Subject: Re: confused constructing a regex
Message-Id: <3g5vvpFarq2dU1@individual.net>
* leeg schrieb:
> I have an input file of a format that looks something like this:
>
> {
> foo = (
> {
> bar = "baz";
> wibble = WOBBLE;
> },
> {
> bar = "barney";
> wibble = JELLY;
> }
> );
> someKey = someValue;
> someArray = (value1, value2);
> blankDict = {};
> };
>
> I've noticed (and at the time was fairly proud of said epiphany) that
> this is almost a declaration of an anonymous hash and with a little
> tweaking I could eval it as such. However, I need to quote it properly,
> and despite a number of attempts can't construct a regex that will do it.
> I want to search for a list of characters which are not the various
> formatting characters [^\(\){};,=] *and* are not already surrounded by
> quotes, and then surround them by quotes.
Well, with your given example, I'd do something like
my $data = do { local $/; <DATA> };
$data =~ s/(["']?)(\w+)\1?/'$2'/g; # fix quotes
$data =~ y/();=/[],,/; # fix arrays and lists
Afterwards, you could eval() it.
regards,
fabian
------------------------------
Date: 1 Jun 2005 16:31:05 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: confused constructing a regex
Message-Id: <d7kns9$a9$1@mamenchi.zrz.TU-Berlin.DE>
leeg <uk.ac.ox.physics.teaching@leeg.invalid> wrote in comp.lang.perl.misc:
> I have an input file of a format that looks something like this:
>
> {
> foo = (
> {
> bar = "baz";
> wibble = WOBBLE;
> },
> {
> bar = "barney";
> wibble = JELLY;
> }
> );
> someKey = someValue;
> someArray = (value1, value2);
> blankDict = {};
> };
>
> I've noticed (and at the time was fairly proud of said epiphany) that
> this is almost a declaration of an anonymous hash and with a little
> tweaking I could eval it as such. However, I need to quote it properly,
> and despite a number of attempts can't construct a regex that will do it.
> I want to search for a list of characters which are not the various
> formatting characters [^\(\){};,=] *and* are not already surrounded by
> quotes, and then surround them by quotes.
You have more things to change before the expression above is a
Perl-parseable data definition. You'll have to change parentheses () to
brackets [], equal signs = to (fat) commas =>, and most (but not all)
semicolons to commas.
>
> I thought of:
> $line =~ s/[\s\(\){};,=]+([^"\(\){};,=])+[\s\(\){};,=]+/"$1"/g;
^^^^ ^^^^ ^^^^
No need to escape (), they're not special in a character class.
> but this converts the above into:
> {
> foo" "
> {
> bar = "baz";
> wibble"E"
> },
> {
> bar = "barney";
> wibble"Y"
> }
> );
> someKey"e"
> someArray"1"value2);
> blankDict" "
> }
Huh? It doesn't do that for me, and it can't, though it doesn't do what
you want either.
> so isn't what I want. What I especially can't determine is why " =
> someValue;" for instance would be replaced by "e".
No idea.
Distinguishing quoted words from unquoted ones with a regex isn't trivial
(as you have seen). As usual, the solution is to use Perl's other features
to keep the regular expressions simple.
In this case, we could split on quoted words (recognizing *them* isn't
hard), keeping the delimiters. That splits the string into quote-free
parts and quoted words that separate them.
Next, walk through the list, leaving the quoted parts alone, but adding
quotes to *every* word in the quote-free regions. Again, this isn't hard.
Finally, join it all together again.
$text = join '',
map { s/(\w+)/"$1"/g unless /^"/; $_}
split /("\w*?")/s, $text;
I works on well-formed expressions only. Unbalanced quotes confuse it,
and quoted non-words probably too.
Anno
------------------------------
Date: Wed, 1 Jun 2005 11:18:54 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: confused constructing a regex
Message-Id: <slrnd9rnve.274.tadmc@magna.augustmail.com>
leeg <uk.ac.ox.physics.teaching@leeg.invalid> wrote:
> I have an input file of a format that looks something like this:
^^^^^^^^^^^^^^
The devil is in the details with regexes, so "something like" is
likely not good enough to get a useable answer.
Can there be spaces in the already-quoted strings? Your example
has none like that.
Can declarations be broken across lines? eg:
someArray = (value1,
value2);
Can you have values on the RHS that you do NOT what to quote?
etc...
> {
> foo = (
> {
> bar = "baz";
> wibble = WOBBLE;
> },
> {
> bar = "barney";
> wibble = JELLY;
> }
> );
> someKey = someValue;
> someArray = (value1, value2);
> blankDict = {};
> };
That looks pretty Formal (as in Formal Methods).
Is it a "little language"?
If so, then find the grammar for it (or write one for it).
You might be able to get the LHS(s) handled by a simple
s/ = / => /;
and let perl autoquote for you.
You'll need to change (some of?) the parens to squares for
anonymous array elements.
> this is almost a declaration of an anonymous hash and with a little
> tweaking I could eval it as such.
> Could someone offer
> some assistance?
It would become Real Easy if you had a grammar for the data, then
you could simply write a parser for the grammar.
Got a grammar?
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 01 Jun 2005 16:01:26 GMT
From: "Dave" <daveandniki@ntlworld.com>
Subject: Encoding problem with automation of Word by Perl
Message-Id: <qZkne.5780$%h4.3950@newsfe3-win.ntli.net>
In the code snippet below @foutput is an array of 'paragraphs': which are
arrays of 'text items': which are arrays with two elements, the first being
a text string and the second another text string which holds formatting
information (currently 'b' for bold 's' for superscript and '' for normal).
The code works in than it opens a Word document and produces formatted text
therein. The problem is that non-ascii Unicode characters do not transfer
cleanly. I expect that Perl and Word are making different assumptions about
what encoding is in use (Word seems to be recieving utf-8 but interpreting
it as 'code page 1252') but I don't know how change it.
Here is the code snippet (use strict and use warnings are in operation at
the top of the file) :
elsif ($opt{w}) {
use Win32::OLE;
my $word = CreateObject Win32::OLE 'Word.Application' or die $!;
$word->{'Visible'} = 1;
my $document = $word->Documents->Add;
my $selection = $word->Selection;
my $i = 0;
foreach my $para (@foutput) {
$i++; last if $i == 5; # just a few for debugging
foreach (@{$para}) {
if (@{$_}[1] eq "") {
$selection->TypeText(@{$_}[0]);
}
elsif (@{$_}[1] eq "b") {
$selection->Font->{Bold} = 1;
$selection->TypeText(@{$_}[0]);
$selection->Font->{Bold} = 0;
}
elsif (@{$_}[1] eq "s") {
$selection->Font->{Superscript} = 1;
$selection->TypeText(@{$_}[0]);
$selection->Font->{Superscript} = 0;
}
else {
die "Unknown formatting: " . @{$_}[1];
}
}
$selection -> TypeParagraph;
}
------------------------------
Date: Wed, 01 Jun 2005 16:08:16 GMT
From: "Dave" <daveandniki@ntlworld.com>
Subject: Re: Encoding problem with automation of Word by Perl
Message-Id: <Q3lne.5783$%h4.1523@newsfe3-win.ntli.net>
"Dave" <daveandniki@ntlworld.com> wrote in message
news:qZkne.5780$%h4.3950@newsfe3-win.ntli.net...
> In the code snippet below @foutput is an array of 'paragraphs': which are
> arrays of 'text items': which are arrays with two elements, the first
> being a text string and the second another text string which holds
> formatting information (currently 'b' for bold 's' for superscript and ''
> for normal).
>
> The code works in than it opens a Word document and produces formatted
> text therein. The problem is that non-ascii Unicode characters do not
> transfer cleanly. I expect that Perl and Word are making different
> assumptions about what encoding is in use (Word seems to be recieving
> utf-8 but interpreting it as 'code page 1252') but I don't know how change
> it.
>
> Here is the code snippet (use strict and use warnings are in operation at
> the top of the file) :
>
> elsif ($opt{w}) {
> use Win32::OLE;
> my $word = CreateObject Win32::OLE 'Word.Application' or die $!;
> $word->{'Visible'} = 1;
> my $document = $word->Documents->Add;
> my $selection = $word->Selection;
> my $i = 0;
> foreach my $para (@foutput) {
> $i++; last if $i == 5; # just a few for debugging
> foreach (@{$para}) {
> if (@{$_}[1] eq "") {
> $selection->TypeText(@{$_}[0]);
> }
> elsif (@{$_}[1] eq "b") {
> $selection->Font->{Bold} = 1;
> $selection->TypeText(@{$_}[0]);
> $selection->Font->{Bold} = 0;
> }
> elsif (@{$_}[1] eq "s") {
> $selection->Font->{Superscript} = 1;
> $selection->TypeText(@{$_}[0]);
> $selection->Font->{Superscript} = 0;
> }
> else {
> die "Unknown formatting: " . @{$_}[1];
> }
> }
> $selection -> TypeParagraph;
> }
>
Sorry, posted too soon as I found the answer shortly afterwards in the Docs
to Activestate perl. Adding:
Win32::OLE->Option(CP => Win32::OLE::CP_UTF8());below the line use
Win32::OLE;solves the problem.Dave
------------------------------
Date: Wed, 01 Jun 2005 18:12:10 +0300
From: Berk Birand <graffiti@yahoo.com>
Subject: Escaping blog patterns
Message-Id: <1117638731.deb18640a9911b2770fe4c08455e18f1@teranews>
Hi,
I have a question about escaping the spaces in glob patterns. After 45
minutes of debugging attempts, I found out that spaces were interpreted
differently in strings of glob patterns.
Apparently spaces are like OR's, and match several criterias at once.
There's also some information about using literal spaces, but it doesn't
apply to my case.
Suppose I have a string
$glob_pattern = "/mnt/file/some dir/*";
So if I want all files in that directory, I should be able to do
@files = glob($glob_pattern);
However this doesn't work. Not even when I change the double quotes to
single quotes. Apparently one way of doing it is to declare the pattern as:
$glob_pattern = '/mnt/files/"some dir"/*';
In my case, that string ($glob_pattern) is not typed literally,
but it comes from a CGI parameter. I don't know how I would make the
change so that the string "/mnt/file/some dir/" will be interpreted as one
path, instead of two.
I hope I made myself clear. Please let me know if there are some points
that are not understood.
Thanks,
bB
------------------------------
Date: Wed, 1 Jun 2005 11:29:10 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Escaping blog patterns
Message-Id: <slrnd9roim.274.tadmc@magna.augustmail.com>
Berk Birand <graffiti@yahoo.com> wrote:
> I have a question about escaping the spaces in glob patterns.
If you used an alternative to glob() you wouldn't have a question
about escaping the spaces in glob patterns. :-)
> After 45
> minutes of debugging attempts, I found out that spaces were interpreted
> differently in strings of glob patterns.
You could learn the alternative in less than 45 minutes...
perldoc -f opendir
perldoc -f readdir
perldoc -f grep
> $glob_pattern = "/mnt/file/some dir/*";
>
> So if I want all files in that directory, I should be able to do
> In my case, that string ($glob_pattern) is not typed literally,
> but it comes from a CGI parameter.
So you *do* already have taint checking enabled then, right?
# untested
my $dir = '/mnt/file/some dir';
my $pattern = '^\.'; # all files that do not start with dot
opendir DIR, $dir or die "could not open '$dir' $!";
my @files = sort
map { "$dir/$_" }
grep /$pattern/, readdir DIR;
closedir DIR;
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 1 Jun 2005 16:48:52 +0000 (UTC)
From: gl@csdsun1.arlut.utexas.edu (Jay G. Scott)
Subject: Re: lexical analyzer generators
Message-Id: <d7kotk$t0q$1@ns3.arlut.utexas.edu>
In article <slrnd9ch3c.acu.tadmc@magna.augustmail.com>,
Tad McClellan <tadmc@augustmail.com> wrote:
>Jay G. Scott <gl@torn.arlut.utexas.edu> wrote:
>>
>> i've asked this before, and i went off an read up on the answers,
>> ie, ParseLex.
>>
>> well, this doesn't help.
>
>
>Yes it does.
>
>
>> i don't see anything out there that's remotely
>> like lex/flex.
>
>
>The module's docs say:
>
> If a sub ref (anonymous sub-
> routine) is given as third argument, it is called when the token is
> recognized. Its arguments are the "Parse::Token" instance and the
> string recognized by the regular expression.
>
>
>> the perl tools seem incapable of the sort of flexibility
>> i want.
>
>
>Generalizing to all Perl Tools based on your misreading of the
>docs for one of the tools is being rather *too* flexible. :-)
not what i meant, of course.
>
>
>> in flex, i can do things like this:
>>
>> aab[cd]+ { C code }
>>
>> acr[a-z][0-9][a-r]* { some other code }
>> [a-z0-9]+ {yet another chunk}
>>
>> whereas i don't see anything akin to this in ParseLex.
>
>
>This works for me:
>
>-----------------------------
>#!/usr/bin/perl
>use warnings;
>use strict;
>use Parse::Lex;
>
>my @token = (
> C => 'aab[cd]+', \&C,
> OTHER => 'acr[a-z][0-9][a-r]*', \&OTHER,
> YAC => '[a-z0-9]+', \&YAC,
> NL => '\n', \&NL,
>);
>
>my $lexer = Parse::Lex->new(@token);
>$lexer->from(\*DATA);
>
>$lexer->next while not $lexer->eoi;
>
>sub C {warn "C() got called with <<$_[1]>>\n"}
>sub OTHER {warn "OTHER() got called with <<$_[1]>>\n"}
>sub YAC {warn "YAC() got called with <<$_[1]>>\n"}
>sub NL {warn "NL() got called with <<$_[1]>>\n"}
not being a perl guru, i doubt i'd have ever figured
this out. certainly i didn't figure it out from the docs
i found.
yeah, i think you've got it for me. thanks.
(i've been on other things for a while, just got back.)
j.
>
>
>__DATA__
>foobar aabdddacrx9
>-----------------------------
>
>
>--
> Tad McClellan SGML consulting
> tadmc@augustmail.com Perl programming
> Fort Worth, Texas
--
Jay Scott 512-835-3553 gl@arlut.utexas.edu
Head of Sun Support, Sr. Operating Systems Specialist
Applied Research Labs, Computer Science Div. S224
University of Texas at Austin
------------------------------
Date: Wed, 01 Jun 2005 18:44:32 +0300
From: Berk Birand <graffiti@yahoo.com>
Subject: Neat way of setting default values
Message-Id: <1117640672.0c165114a8bc10337e8b8927e7a9577f@teranews>
Hi,
I want a smart and compact way to set the default value of a scalar
variable in case a function returns undef. I wrote this program which uses
MP3-Info to read the ID3 Tag of an mp3 file. The main code looks like:
my $tag = (get_mp3tag($file) or "No tag\n");
using the "or die" construct is so common that I thought this would work,
but it doesn't.
How do I set the value this way?
Actually I think I was mislead by the LISP "or" syntax,
which returns the last argument's value (that also explains why I put
parens around the code :D ).
Thanks,
bB
------------------------------
Date: 01 Jun 2005 15:47:32 GMT
From: "Mark Clements" <mark.clementsREMOVETHIS@wanadoo.fr>
Subject: Re: Neat way of setting default values
Message-Id: <429dd894$0$3138$8fcfb975@news.wanadoo.fr>
Berk Birand wrote:
> Hi,
>
> I want a smart and compact way to set the default value of a scalar
> variable in case a function returns undef. I wrote this program which uses
> MP3-Info to read the ID3 Tag of an mp3 file. The main code looks like:
>
> my $tag = (get_mp3tag($file) or "No tag\n");
>
> using the "or die" construct is so common that I thought this would work,
> but it doesn't.
>
> How do I set the value this way?
>
> Actually I think I was mislead by the LISP "or" syntax,
> which returns the last argument's value (that also explains why I put
> parens around the code :D ).
use strict;
use warnings;
use constant DEFAULT_VALUE => 42;
# rest of script
my $testVar = somefunction() || DEFAULT_VALUE;
Mark
------------------------------
Date: Wed, 01 Jun 2005 17:52:30 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Neat way of setting default values
Message-Id: <3g63rqFarbn6U1@individual.net>
Berk Birand wrote:
> I want a smart and compact way to set the default value of a scalar
> variable in case a function returns undef. I wrote this program which uses
> MP3-Info to read the ID3 Tag of an mp3 file. The main code looks like:
>
> my $tag = (get_mp3tag($file) or "No tag\n");
Why isn't that smart enough?
> using the "or die" construct is so common that I thought this would work,
> but it doesn't.
The "or die" construct works fine for me. What has that to do with your
problem?
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Wed, 01 Jun 2005 18:54:55 +0300
From: Berk Birand <graffiti@yahoo.com>
Subject: Re: Neat way of setting default values
Message-Id: <1117641295.10f54446b58b38927f2b3b16600385cc@teranews>
On Wed, 01 Jun 2005 17:52:30 +0200, Gunnar Hjalmarsson wrote:
> Berk Birand wrote:
>> I want a smart and compact way to set the default value of a scalar
>> variable in case a function returns undef. I wrote this program which uses
>> MP3-Info to read the ID3 Tag of an mp3 file. The main code looks like:
>>
>> my $tag = (get_mp3tag($file) or "No tag\n");
>
> Why isn't that smart enough?
It is not smart enough because it doesn't work.
>
>> using the "or die" construct is so common that I thought this would work,
>> but it doesn't.
>
> The "or die" construct works fine for me. What has that to do with your
> problem?
The "or die" construct works for everyone not just for you. I want a
similar construct to set a default value for a variable.
Thanks for the answer,
bB
------------------------------
Date: Wed, 01 Jun 2005 18:23:57 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Neat way of setting default values
Message-Id: <3g65opFau74qU1@individual.net>
Berk Birand wrote:
> Gunnar Hjalmarsson wrote:
>> Berk Birand wrote:
>>> my $tag = (get_mp3tag($file) or "No tag\n");
>>
>> Why isn't that smart enough?
>
> It is not smart enough because it doesn't work.
It works fine. So does:
my $tag = get_mp3tag($file) || "No tag\n";
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Wed, 01 Jun 2005 18:35:15 +0200
From: Xenna <xennar@yahoo.com>
Subject: Re: Neat way of setting default values
Message-Id: <429de387$0$18858$e4fe514c@dreader17.news.xs4all.nl>
Gunnar Hjalmarsson wrote:
> Berk Birand wrote:
>
>> Gunnar Hjalmarsson wrote:
>>
>>> Berk Birand wrote:
>>>
>>>> my $tag = (get_mp3tag($file) or "No tag\n");
>>>
>>>
>>> Why isn't that smart enough?
>>
>>
>> It is not smart enough because it doesn't work.
>
>
> It works fine. So does:
>
> my $tag = get_mp3tag($file) || "No tag\n";
Sometimes those of us who are great at communicating with computers are
not so great at communicating with people.
Instead of kindly telling you what they know you want to hear they revel
in explaining you how you stupidly misphrased your questions.
It's not Perl-specific though ;)
X.
------------------------------
Date: Wed, 1 Jun 2005 11:35:24 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Neat way of setting default values
Message-Id: <slrnd9rouc.274.tadmc@magna.augustmail.com>
Berk Birand <graffiti@yahoo.com> wrote:
> On Wed, 01 Jun 2005 17:52:30 +0200, Gunnar Hjalmarsson wrote:
>
>> Berk Birand wrote:
>>> I want a smart and compact way to set the default value of a scalar
>>> variable in case a function returns undef. I wrote this program which uses
>>> MP3-Info to read the ID3 Tag of an mp3 file. The main code looks like:
>>>
>>> my $tag = (get_mp3tag($file) or "No tag\n");
>>
>> Why isn't that smart enough?
>
> It is not smart enough because it doesn't work.
If you post a short and complete program *that we can run* that
illustrates the "doesn't work", then we could surely help solve
your problem.
It works for me:
--------------------
#!/usr/bin/perl
use warnings;
use strict;
my $tag;
$tag = (get_mp3tag( 1 ) or "No tag\n");
print "true gets '$tag'\n";
$tag = (get_mp3tag( 0 ) or "No tag\n");
print "false gets '$tag'\n";
sub get_mp3tag { return $_[0] }
--------------------
If it isn't working for you then the problem is in the definition
for get_mp3tag(), which you haven't shown to us.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 01 Jun 2005 18:52:14 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Neat way of setting default values
Message-Id: <3g67fmFasbe2U1@individual.net>
Xenna wrote:
> Gunnar Hjalmarsson wrote:
>> Berk Birand wrote:
>>> Gunnar Hjalmarsson wrote:
>>>> Berk Birand wrote:
>>>>>
>>>>> my $tag = (get_mp3tag($file) or "No tag\n");
>>>>
>>>> Why isn't that smart enough?
>>>
>>> It is not smart enough because it doesn't work.
>>
>> It works fine. So does:
>>
>> my $tag = get_mp3tag($file) || "No tag\n";
>
> Sometimes those of us who are great at communicating with computers are
> not so great at communicating with people.
>
> Instead of kindly telling you what they know you want to hear they revel
> in explaining you how you stupidly misphrased your questions.
Even if that may be true once in a while, I don't understand how it
would be applicable in this case. How did you figure out what the OP
"wants" to know? I still don't know what it is...
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Wed, 01 Jun 2005 18:59:03 +0300
From: Berk Birand <graffiti@yahoo.com>
Subject: Re: Neat way of setting default values[SOLVED]
Message-Id: <1117641545.7b12e9296ea557040068244211e22a0b@teranews>
On Wed, 01 Jun 2005 15:47:32 +0000, Mark Clements wrote:
> use strict;
> use warnings;
>
> use constant DEFAULT_VALUE => 42;
>
> # rest of script
>
> my $testVar = somefunction() || DEFAULT_VALUE;
I see. Well it turns out that I was using the wrong OR operator. What I
wanted is the || so I can do:
my $var = get_mp3tag($file) || "Nope.";
I will read some more about the difference between the various logic
operators.
Thanks for your answer!
bB
------------------------------
Date: Wed, 01 Jun 2005 17:48:18 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Neat way of setting default values[SOLVED]
Message-Id: <d7kosh$ell$2@redhat2.bham.ac.uk>
Berk Birand wrote:
> I see. Well it turns out that I was using the wrong OR operator. What I
> wanted is the || so I can do:
>
> my $var = get_mp3tag($file) || "Nope.";
>
> I will read some more about the difference between the various logic
> operators.
There is no difference except precedence.
The follow (AFIAK) compile to the same bytecode.
my $var = get_mp3tag($file) || "Nope.";
my $var = ( get_mp3tag($file) or "Nope." );
------------------------------
Date: Wed, 01 Jun 2005 11:24:12 -0500
From: Moltar <klubbheads_NO_SPAM@rogers.com>
Subject: Re: Parsing Tracklisting - discussion
Message-Id: <Xns96687E5B8295D86745413465432435464@216.196.97.142>
Why can't I just use plain text file with 1 regex per line?
Are there any obstacles?
Is Config::Properties faster?
"Mark Clements" <mark.clementsREMOVETHIS@wanadoo.fr> wrote in news:429d3d49$0$1248
$8fcfb975@news.wanadoo.fr:
> Config::Properties
------------------------------
Date: Wed, 01 Jun 2005 13:53:25 -0400
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: perl style: can I combine two steps into one?
Message-Id: <m2psv655gq.fsf@ubiquity.chromatico.net>
>>>>> "DF" == David Formosa (aka ? the Platypus)
>>>>> <dformosa@dformosa.zeta.org.au> writes:
>> I lump together closing brackets of all kind, unless I want one
>> to stand out. You don't want to miss an opening bracket, but
>> the closing ones only confirm what you know. A lump of them
>> can be parsed as "this closes everything" (up to a point that
>> can be indicated through indentation).
DF> Likewise, and like you I've had Lisp esposure.
I do that in LISP. I don't do it anywhere else, because I find that
in languages like Perl and C the vertical organization of code and the
lines of mostly-whitespace that happen when you have braces on a line
by themselves really do help me follow what's going on; and the angled
nested closing braces, each one on its own line, are also helpful.
I'm working now for a company doing some PHP work, and the combination
of optional braces and the K&R/Java-like parsimony of vertical spacing
in their house style means I've been *extremely* grateful for Emacs's
brace-matching.
Charlton
--
cwilbur at chromatico dot net
cwilbur at mac dot com
------------------------------
Date: 1 Jun 2005 08:31:00 -0700
From: djb@global.net.mt (David Joseph Bonnici)
Subject: Suppression of error messages if a regex does not match
Message-Id: <52e74f9f.0506010731.2bbb1e45@posting.google.com>
Whenever I have a capture group that does not produce a match I am getting loads of
"Use of unitialized valie in string ne at "
Is there a way how I can suppress these error messages.
Many Thanks and
Kind Regards
David Joseph Bonnici
------------------------------
Date: Wed, 01 Jun 2005 17:41:31 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Suppression of error messages if a regex does not match
Message-Id: <3g636oFauibpU1@individual.net>
David Joseph Bonnici wrote:
> Whenever I have a capture group that does not produce a match I am getting loads of
> "Use of unitialized valie in string ne at "
> Is there a way how I can suppress these error messages.
They are warnings, not errors.
if ( /([a-z]+)/ ) {
print $1 ne 'foo' ? "Okay\n" : "Not okay\n";
}
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: 01 Jun 2005 15:45:06 GMT
From: "Mark Clements" <mark.clementsREMOVETHIS@wanadoo.fr>
Subject: Re: Suppression of error messages if a regex does not match
Message-Id: <429dd802$0$25045$8fcfb975@news.wanadoo.fr>
David Joseph Bonnici wrote:
> Whenever I have a capture group that does not produce a match I am getting loads of
> "Use of unitialized valie in string ne at "
> Is there a way how I can suppress these error messages.
You should always be testing for a successful match before using the captured match variables.
eg
if($testString =~/\b(\d+)\b/){
my $num = $1;
}
However, you can also check for definedness first:
my $capture = $1;
if(defined $capture && $capture ne "teststring"){
}
And (you probably don't want to be doing this), you can turn off warnings for a block.
See
perldoc warnings
Mark
------------------------------
Date: Wed, 1 Jun 2005 18:05:37 +0100
From: "Alan J. Flavell" <flavell@ph.gla.ac.uk>
Subject: Re: Suppression of error messages if a regex does not match
Message-Id: <Pine.LNX.4.62.0506011735290.12211@ppepc56.ph.gla.ac.uk>
On Wed, 1 Jun 2005, David Joseph Bonnici wrote:
> "Use of unitialized valie in string ne at "
*Do* please get into the habit of copy/pasting the exact text of what
you are seeing. Re-typing it (badly) is no way to get help, and soon
you are going to find problems here.
Perl has a list of the diagnostic (error and warning) messages which
it issues itself (of which this is one), and you can look them up for
an explanation. The documentation comes with every Perl installation,
and there are also copies on the web.
perldoc perldiag
Conversely, I find that feeding an exact error message to a search
engine (e.g google) can often solve my own problems much faster than
posting to a usenet group, but mistyping the message pretty-much
guarantees very poor results from the search.
> Is there a way how I can suppress these error messages.
You need to understand what you are seeing here. Perl is alerting you
to a potential problem, by means of a warning (not literally "error")
message.
So what do you really want to do? Make good use of Perl's help (which
is what I would recommend), or hide the problem away and hope for the
best?
The truth is that your program logic is faulty: you are trying to use
a variable to which you have not assigned a value. Perl is helpfully
alerting you to this. My advice is don't even think about suppressing
this valuable information: use it, work out what it's telling you.
Consider yourself lucky, that the failed match didn't leave some
unrelated value from a previous operation, which you would have used
without any warning. Correct your program logic so that you don't try
to use a match value when it hasn't been set.
If and when you're in a situation where you *really* want a variable
set to an empty value rather than being undefined, do what the
documentation says:
Use of uninitialized value%s
An undefined value was used as if it were already defined.
It was interpreted as a "" or a 0, but maybe it was a mistake.
To suppress this warning assign a defined value to your variables.
Which is a good answer, in appropriate situations. But that's not
what's needed here (regex match variables).
Anyone who advises turning warnings off to hide the warning may be
technically accurate about what Perl does or can do - but
operationally it's a disaster. The whole point of Perl's warnings and
strict pragmas are to help one find errors on one's code. Only in the
most extreme cases should it be necessary to turn these off briefly.
Consider all other possibilities first. That's my advice.
------------------------------
Date: Wed, 01 Jun 2005 15:19:18 +0200
From: Huub <hdotvdotniekerkathccnetdotnl>
Subject: Re: writing to file
Message-Id: <429db5a1$0$776$3a628fcd@reader20.nntp.hccnet.nl>
Thank you for this explanation. Writing to file is working now.
Huub
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 8139
***************************************