[30092] in Perl-Users-Digest
Perl-Users Digest, Issue: 1335 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Mar 5 18:10:02 2008
Date: Wed, 5 Mar 2008 15:09:05 -0800 (PST)
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, 5 Mar 2008 Volume: 11 Number: 1335
Today's topics:
Re: CGI file based logging xhoster@gmail.com
Re: CGI file based logging <john@castleamber.com>
Re: Fail extracting table from .mdb file using DBI modu (David Combs)
Re: Fail extracting table from .mdb file using DBI modu <uri@stemsystems.com>
Re: Fail extracting table from .mdb file using DBI modu <jimsgibson@gmail.com>
Re: Fail extracting table from .mdb file using DBI modu xhoster@gmail.com
Gtk2 IRC Client Sluggish and not Responsive <deadpickle@gmail.com>
Re: Help with HTML:TableContentParser (David Combs)
Re: Help with HTML:TableContentParser <uri@stemsystems.com>
Parsing blocks of text in Perl <ernest.mueller@ni.com>
Re: Parsing blocks of text in Perl <mgjv@tradingpost.com.au>
Re: Parsing blocks of text in Perl <noreply@gunnar.cc>
Re: Parsing blocks of text in Perl <ernest.mueller@ni.com>
Re: Parsing blocks of text in Perl <ernest.mueller@ni.com>
Re: Secure FTP <syscjm@sumire.gwu.edu>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 05 Mar 2008 17:56:46 GMT
From: xhoster@gmail.com
Subject: Re: CGI file based logging
Message-Id: <20080305125648.181$kt@newsreader.com>
John Bokma <john@castleamber.com> wrote:
> xhoster@gmail.com wrote:
>
> [locking a log file]
> > In that case, why bother locking at all?
>
> Because you don't want to mess up your log file? There is a difference
> between missing a log entry and a messed up log file.
How messed up is the log file going to get? The worst I've ever seen
is one partially truncated line, which is about the same level of
badness as a missing entry in my book.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
------------------------------
Date: 5 Mar 2008 18:51:51 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: CGI file based logging
Message-Id: <Xns9A5882DC66CA1castleamber@130.133.1.4>
xhoster@gmail.com wrote:
> John Bokma <john@castleamber.com> wrote:
>> xhoster@gmail.com wrote:
>>
>> [locking a log file]
>> > In that case, why bother locking at all?
>>
>> Because you don't want to mess up your log file? There is a difference
>> between missing a log entry and a messed up log file.
>
> How messed up is the log file going to get? The worst I've ever seen
> is one partially truncated line, which is about the same level of
> badness as a missing entry in my book.
I guess we have to agree on having different books ;-)
--
John
http://johnbokma.com/mexit/2008/02/
------------------------------
Date: Wed, 5 Mar 2008 17:24:58 +0000 (UTC)
From: dkcombs@panix.com (David Combs)
Subject: Re: Fail extracting table from .mdb file using DBI module
Message-Id: <fqml1a$blo$1@reader2.panix.com>
In article <x7ir0xgwy1.fsf@mail.sysarch.com>,
Uri Guttman <uri@stemsystems.com> wrote:
...
>
> M> my $dsn = "dbi:ODBC:driver=$driver;dbq=$database";
> M> my $dbh = DBI->connect("$dsn") or warn "Couldn't open database:
>
>
>don't quote single variables like $dsn as it is useless and may actually
>cause bugs.
Interesting. How so?
Thanks,
David
------------------------------
Date: Wed, 05 Mar 2008 17:44:42 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Fail extracting table from .mdb file using DBI module
Message-Id: <x7bq5t83qt.fsf@mail.sysarch.com>
>>>>> "DC" == David Combs <dkcombs@panix.com> writes:
DC> In article <x7ir0xgwy1.fsf@mail.sysarch.com>,
DC> Uri Guttman <uri@stemsystems.com> wrote:
DC> ...
>>
M> my $dsn = "dbi:ODBC:driver=$driver;dbq=$database";
M> my $dbh = DBI->connect("$dsn") or warn "Couldn't open database:
>>
>>
>> don't quote single variables like $dsn as it is useless and may actually
>> cause bugs.
DC> Interesting. How so?
google for many postings about why unneeded quoting can be bad and
slow. no need to repeat it again.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Architecture, Development, Training, Support, Code Review ------
----------- Search or Offer Perl Jobs ----- http://jobs.perl.org ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Wed, 05 Mar 2008 09:50:51 -0800
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: Fail extracting table from .mdb file using DBI module
Message-Id: <050320080950511258%jimsgibson@gmail.com>
In article <fqml1a$blo$1@reader2.panix.com>, David Combs
<dkcombs@panix.com> wrote:
> In article <x7ir0xgwy1.fsf@mail.sysarch.com>,
> Uri Guttman <uri@stemsystems.com> wrote:
>
> ...
>
> >
> > M> my $dsn = "dbi:ODBC:driver=$driver;dbq=$database";
> > M> my $dbh = DBI->connect("$dsn") or warn "Couldn't open database:
> >
> >
> >don't quote single variables like $dsn as it is useless and may actually
> >cause bugs.
>
> Interesting. How so?
See 'perldoc -q quoting' "What's wrong with always quoting "$vars"?"
--
Jim Gibson
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
------------------------------
Date: 05 Mar 2008 18:04:23 GMT
From: xhoster@gmail.com
Subject: Re: Fail extracting table from .mdb file using DBI module
Message-Id: <20080305130424.764$DY@newsreader.com>
Jim Gibson <jimsgibson@gmail.com> wrote:
> In article <fqml1a$blo$1@reader2.panix.com>, David Combs
> <dkcombs@panix.com> wrote:
>
> > In article <x7ir0xgwy1.fsf@mail.sysarch.com>,
> > Uri Guttman <uri@stemsystems.com> wrote:
> >
> > ...
> >
> > >
> > > M> my $dsn = "dbi:ODBC:driver=$driver;dbq=$database";
> > > M> my $dbh = DBI->connect("$dsn") or warn "Couldn't open
> > > database:
> > >
> > >
> > >don't quote single variables like $dsn as it is useless and may
> > >actually cause bugs.
> >
> > Interesting. How so?
>
> See 'perldoc -q quoting' "What's wrong with always quoting "$vars"?"
OK. So? Do we use ++ on DSNs? Do we use references for DSNs?
Always quoting "$vars" is different than quoting "$vars" at least once.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
------------------------------
Date: Wed, 5 Mar 2008 14:23:52 -0800 (PST)
From: deadpickle <deadpickle@gmail.com>
Subject: Gtk2 IRC Client Sluggish and not Responsive
Message-Id: <1e113b57-d355-4464-b6a4-5892f8c7222d@u10g2000prn.googlegroups.com>
I am writing a Gtk2 IRC client (and have been for months now) and have
been having problems with it. The problems keep occuring as I try to
figure out a good way to handle incoming messages as well as out
going. One module Parse::IRC has done wonders in organizing the server
outputs. Right now the program starts a Glib::Timeout on connection
then IO::Selects the socket and listens to see if the socket can be
read. This is causing the Gtk2 part of the program to be sluggish and
non-responsive but the terminal window receives inputs just fine. What
I'm looking for is a way to make this program work smoother (and
please dont suggest using Net::IRC it has the same problems).
#!/usr/local/bin/perl -w
use strict;
use Gtk2 '-init';
use Glib qw/TRUE FALSE/;
use Parse::IRC;
use IO::Select;
my $chat_state = 'Connect';
my $channel = "#GRRUVI";
my $chat_entry;
my $chat_send_sig;
my $chat_textview;
my $chat_button;
my $tag;
my $job = 3;
my $sock;
my $parser = Parse::IRC->new( public => 1 );
my %dispatch = ( 'ping' => \&irc_ping, '001' => \&irc_001, 'public' =>
\&irc_public, 'quit' => \&irc_quit, 'join' => \&irc_join, '353' =>
\&irc_353);
my $irc = "irc.servercentral.net";
my $nick = "MetBase";
my $login = $nick;
my $post = 0;
my $msg;
my $who;
my $text;
my $sel = IO::Select->new();
#################################################
#Threads
#my $thread_chat = threads->new( \&chat);
#################################################
#-------------------Main Loop-------------------
&chat_build;
Gtk2->main;
####################CHAT BLOCK####################
#-------------------chat Build-------------------
sub chat_build {
my $chat_window = Gtk2::Window->new('toplevel');
$chat_window->set_title('Chat Client');
$chat_window->set_position('center');
$chat_window->set_default_size( 300, 300 );
$chat_window->signal_connect(delete_event=> sub{
Gtk2->main_quit;
});
$chat_entry = Gtk2::Entry->new;
my $chat_vbox = Gtk2::VBox->new;
#*********Removed Menu and Shortcuts*********#
my $chat_scroll = Gtk2::ScrolledWindow->new;
$chat_scroll->set_shadow_type( 'etched-out');
$chat_textview = Gtk2::TextView->new;
#$chat_entry = Gtk2::Entry->new;
my $chat_buffer = $chat_textview->get_buffer;
$chat_buffer->create_mark( 'end', $chat_buffer->get_end_iter,
FALSE );
$chat_buffer->signal_connect(insert_text => sub {
$chat_textview->scroll_to_mark( $chat_buffer->get_mark('end'),
0.0, TRUE, 0, 0.5 );
});
$chat_button = Gtk2::Button->new;
$chat_button->set_label($chat_state);
# allows for sending each line with an enter keypress
$chat_send_sig = $chat_entry->signal_connect ('key-press-event' =>
sub {
my ($widget,$event)= @_;
if( $event->keyval() == 65293){ # a return key press
my $text = $chat_entry->get_text;
print $sock "PRIVMSG $channel :$text\r\n";
$chat_entry->set_text('');
$chat_entry->set_position(0);
}
});
$chat_entry->signal_handler_block($chat_send_sig); #not connected
yet
$chat_entry->set_editable(0);
$chat_textview->set_editable(0);
$chat_textview->set_cursor_visible(0);
$chat_textview->set_wrap_mode('word');
$chat_scroll->add($chat_textview);
$chat_vbox->add($chat_scroll);
$chat_vbox->pack_start( $chat_entry, FALSE, FALSE, 0 );
$chat_vbox->pack_start( $chat_button, FALSE, FALSE, 0 );
$chat_window->add($chat_vbox);
$chat_window->show_all;
$chat_button->signal_connect("clicked" => sub {
if ($chat_state eq 'Connect') {
$chat_button->set_label('Disconnect');
$chat_state='Disconnect';
#$chat_start = 1;
&connecting;
}
else {
$chat_button->set_label('Connect');
$chat_state='Connect';
#$chat_start = 0;
}
});
#*********Removed Keyboard shortcuts and Output File
Creation*********#
return 1;
}
#-------------------Connect to IRC Server-------------------
sub connecting {
# Connect to the IRC server.
$sock = new IO::Socket::INET(
PeerAddr => $irc,
PeerPort => 6667,
Proto => 'tcp',
) or die "Can't connect\n";
print $sock "NICK $nick\r\n";
print $sock "USER $login 8 * :CoCoNUE Member $nick\r\n";
$sel->add($sock);
#################################################
#Timeout
my $timer_chat = Glib::Timeout->add(0.5, \&incoming_data);
#################################################
return 1;
}
#-------------------Watch for IRC Inputs-------------------
sub incoming_data {
if ($sel->can_read(1)){
print "got something\n";
my $input = <$sock>;
$input =~ s/\r\n//g;
my $hashref = $parser->parse( $input );
# print "2\n";
SWITCH: {
# print "3\n";
my $type = lc $hashref->{command};
my @args;
push @args, $hashref->{prefix} if $hashref->{prefix};
push @args, @{ $hashref->{params} };
if ( defined $dispatch{$type} ) {
# print "4\n";
$dispatch{$type}->(@args);
last SWITCH;
}
print STDOUT join( ' ', "irc_$type:", @args ), "\n";
# print "5\n";
}
}
return 1;
}
#*********Removed Message Posting in the GTK2 Window*********#
####################Chat parsers###################
#-------------------Ping-------------------
sub irc_ping {
my $server = shift;
print $sock "PONG :$server\r\n";
return 1;
}
#-------------------initial login-------------------
sub irc_001 {
$msg = $_[0];
$post = 2;
print STDOUT "Connected to $_[0]\n";
print $sock "JOIN $channel\r\n";
return 1;
}
#-------------------Public-------------------
sub irc_public {
my @sender = split(/!/, $_[0]);
$who = $sender[0];
$msg = $_[2];
$post = 5;
print "$who ->$msg \n";
return 1;
}
#-------------------Quit-------------------
sub irc_quit {
my @sender = split(/!/, $_[0]);
$who = $sender[0];
$msg = $channel;
$post = 4;
print "$who This is QUIT\n";
return 1;
}
#-------------------Join-------------------
sub irc_join {
my @sender = split(/!/, $_[0]);
$who = $sender[0];
$msg = $channel;
$post = 3;
print "$who This is Join\n";
return 1;
}
#-------------------List Users-------------------
sub irc_353 {
my ($who,$where,$what) = @_;
$msg = $_[4];
$post = 7;
print "$_[0] -> $_[1] -> $_[2] -> $_[3] -> $_[4]\n";
return 1;
}
#################################################
------------------------------
Date: Wed, 5 Mar 2008 16:59:57 +0000 (UTC)
From: dkcombs@panix.com (David Combs)
Subject: Re: Help with HTML:TableContentParser
Message-Id: <fqmjic$s18$1@reader2.panix.com>
In article <x77ihgv2be.fsf@mail.sysarch.com>,
Uri Guttman <uri@stemsystems.com> wrote:
>>>>>> "r" == roberthuberjr <roberthuberjr@gmail.com> writes:
>
> r> my $modules = $tables->[1];
> r> foreach my $r (@{ $modules->{rows} })
> r> {
> r> my ($date_time, $sip, $dip, $protocol, $sport, $dport,
> r> $tcpflags, $tos, $num_pkts, $num_octets, $srcasn, $dstasn) =
> r> parse_module_row($r, $FILE);
>
>GACK!!
>
>use a hash or something.
Probably totally obvious, but could you show how you'd simplify
that with a hash? Thanks!
> returning long lists of scalars is bug prone
>and painful to read.
>
> r> sub parse_module_row
> r> {
> r> my ($row, $FILE) = @_;
> r> my ($date_time, $sip, $dip, $protocol, $sport, $dport,
> r> $tcpflags, $tos, $num_pkts, $num_octets, $srcasn, $dstasn);
>
>declare those where you need them
>
> r> $date_time = $row->{cells}[0]{data};
>
> my $date_time = $row->{cells}[0]{data};
>
> r> $sip = $row->{cells}[1]{data};
> r> $dip = $row->{cells}[2]{data};
> r> $protocol = $row->{cells}[3]{data};
> r> $sport = $row->{cells}[4]{data};
> r> $dport = $row->{cells}[5]{data};
> r> $tcpflags = $row->{cells}[6]{data};
> r> $tos = $row->{cells}[7]{data};
> r> $num_pkts = $row->{cells}[8]{data};
> r> $num_octets = $row->{cells}[9]{data};
> r> $srcasn = $row->{cells}[10]{data};
> r> $dstasn = $row->{cells}[10]{data};
>
>do you notice any repetition in that code? i can barely see the
>differences as it is almost all repeated code.
>
>the last two are the same data. is that a bug? another reason to not do
>repeated lines where you edit each one slightly
>
>first, factor out $row->{cells} into $cells
>
> my $cells = $row->{cells} ;
>
>then you can use a hash to get all the data with a simpler slice and
>map:
>
># declare this outside the sub:
>
> my( @fields ) = qw(
> date_time sip dip protocol sport dport tcpflags tos
> num_pkts num_octets srcasn dstasn
> ) ;
>
> my %row ;
> @row{ @fields } = map $cells->[$_]{data}, 0 .. $#fields ;
>
Again probably totally (or mostly) obvious, but you explain
the above line a bit?
Perhaps also "expand" it to show any implicit thus missing right-arrows.
Thanks!
David
------------------------------
Date: Wed, 05 Mar 2008 17:54:08 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Help with HTML:TableContentParser
Message-Id: <x77igh83b4.fsf@mail.sysarch.com>
>>>>> "DC" == David Combs <dkcombs@panix.com> writes:
DC> In article <x77ihgv2be.fsf@mail.sysarch.com>,
DC> Uri Guttman <uri@stemsystems.com> wrote:
>>>>>>> "r" == roberthuberjr <roberthuberjr@gmail.com> writes:
>>
r> my $modules = $tables->[1];
r> foreach my $r (@{ $modules->{rows} })
r> {
r> my ($date_time, $sip, $dip, $protocol, $sport, $dport,
r> $tcpflags, $tos, $num_pkts, $num_octets, $srcasn, $dstasn) =
r> parse_module_row($r, $FILE);
>>
>> GACK!!
>>
>> use a hash or something.
DC> Probably totally obvious, but could you show how you'd simplify
DC> that with a hash? Thanks!
the code is below. you even ask to explain that more.
>> first, factor out $row->{cells} into $cells
>>
>> my $cells = $row->{cells} ;
>>
>> then you can use a hash to get all the data with a simpler slice and
>> map:
>>
>> # declare this outside the sub:
>>
>> my( @fields ) = qw(
>> date_time sip dip protocol sport dport tcpflags tos
>> num_pkts num_octets srcasn dstasn
>> ) ;
>>
>> my %row ;
>> @row{ @fields } = map $cells->[$_]{data}, 0 .. $#fields ;
>>
DC> Again probably totally (or mostly) obvious, but you explain
DC> the above line a bit?
it is just grabbing a list of field values via the map and its index
list argument. this was done in the long set of redundant assignments in
the OP's code. same code, just replaced the integer with $_. the left
side is just a simple assignment to hash slice of the field names. it is
actually very simple as the original code was simple. read more about
hash slices at:
http://sysarch.com/Perl/hash_slice.txt
DC> Perhaps also "expand" it to show any implicit thus missing right-arrows.
no, the code is simple enough. and there is only one implicit right
arrow in the only place it could be. i leave locating that as an exercise.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Architecture, Development, Training, Support, Code Review ------
----------- Search or Offer Perl Jobs ----- http://jobs.perl.org ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Wed, 5 Mar 2008 12:35:43 -0800 (PST)
From: mxyzplk <ernest.mueller@ni.com>
Subject: Parsing blocks of text in Perl
Message-Id: <b41a3793-017d-4cf6-af2f-bf1b3b01a32d@s8g2000prg.googlegroups.com>
OK, so every way I've thought of doing this is really ugly. I'm using
Perl 5.8.4 and only have access to the stock libraries, mostly.
What I need to do is parse through a text file and perform some
transformations on embedded link structures for a wiki content
conversion. A "link" is defined as anything wrapped in double
brackets - [[<string>]], which can appear anywhere in a line of text
and multiple links can appear in a line of text.
1) If the link has a colon (":") in it, I need to strip out all
special characters and spaces (everything except [a-zA_Z0-9]) from the
portion before the colon but leave the part after the colon intact.
Examples:
[[Operation Intranet 2.0!:EvalHome|Eval Home]] -->
[[OperationIntranet20:EvalHome|Eval Home]]
[[UP Platform:Home|UP Platform]] --> [[UPPlatform:Home|UP Platform]]
2) If the link does not have a ":" in it, I need to insert the string
General: before the name of the page.
Examples:
[[Technical FAQs|Technical FAQs]] --> [[General:Technical FAQs|
Technical FAQs]]
[[Embedded - Top 5 content|Top 5 content]] [[General:Embedded - Top 5
content|Top 5 content]]
3) Special case - don't change if it is an image link or if it is an
external link (only single [] enclosure).
Examples:
[[Image:BIhouse.jpg]] --> [[Image:BIhouse.jpg]]
[http://spss.wikicities.com/wiki/SPSS_Wiki SPSS Wiki] --> [http://
spss.wikicities.com/wiki/SPSS_Wiki SPSS Wiki]
I expect this is similar to some HTML parsing requirements, but I've
been hunting through my O'Reilly Perl books and Googling and I'm
having trouble finding my way. Normal regexp replace appears not to
be the way to go and I'm having greediness issues. Ideas?
Thanks,
Ernest
------------------------------
Date: Thu, 6 Mar 2008 08:05:58 +1100
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: Parsing blocks of text in Perl
Message-Id: <slrnfsu2pm.8gr.mgjv@martien.heliotrope.home>
On Wed, 5 Mar 2008 12:35:43 -0800 (PST),
mxyzplk <ernest.mueller@ni.com> wrote:
> OK, so every way I've thought of doing this is really ugly. I'm using
> Perl 5.8.4 and only have access to the stock libraries, mostly.
>
> What I need to do is parse through a text file and perform some
> transformations on embedded link structures for a wiki content
> conversion. A "link" is defined as anything wrapped in double
> brackets - [[<string>]], which can appear anywhere in a line of text
> and multiple links can appear in a line of text.
This implies that they cannot span more than one line of text, which is
what I assumed.
> 1) If the link has a colon (":") in it, I need to strip out all
> special characters and spaces (everything except [a-zA_Z0-9]) from the
> portion before the colon but leave the part after the colon intact.
> 2) If the link does not have a ":" in it, I need to insert the string
> General: before the name of the page.
> 3) Special case - don't change if it is an image link or if it is an
> external link (only single [] enclosure).
> Examples:
> [[Image:BIhouse.jpg]] --> [[Image:BIhouse.jpg]]
Removing everything except a-zA-Z0-9 from 'Image' doesn't change it.
> [http://spss.wikicities.com/wiki/SPSS_Wiki SPSS Wiki] --> [http://
> spss.wikicities.com/wiki/SPSS_Wiki SPSS Wiki]
Avoiding looking at single brackets would be easiest.
> I expect this is similar to some HTML parsing requirements, but I've
> been hunting through my O'Reilly Perl books and Googling and I'm
> having trouble finding my way. Normal regexp replace appears not to
> be the way to go and I'm having greediness issues. Ideas?
This is not nearly as complex as HTML, unles you haven't yet given us
all possible problems. I'm pretty sure that a regex can do that, and
greediness issues should, in this case, be simply fixable by using
non-greedy modifiers. If you have an example that doesn't get correctly
handled by the below, let us know.
Next time, before you post here, show us what you have tried first. This
is not a place where you can coe to get free code all the time, and if
you don't show us what you have tried, it looks like that is exactly
what you're trying to do.
For this time:
#!/usr/bin/perl
use warnings;
use strict;
while (<>)
{
s/\[\[(.*?)\]\]/'[[' . replace_link($1) . ']]'/ge;
print;
}
sub replace_link
{
my @link = split ':', shift;
if (@link == 1)
{
unshift @link, 'General';
}
else
{
$link[0] =~ tr/a-zA-Z0-9//dc;
}
return join ':', @link;
}
This can probably be made a bit faster, by avoiding splitting and
joining, but unless it's a problem I wouldn't worry about it. The
mechanism remains the same, and you shold be easily able to adjust
replace_link to taste. You could also avoid having to put the brackets
back by using look-(ahead|behind) assertions instead, but I generally
find this more readable. If links can cross line bondaries, and files
aren't too large, read the whole file in, and run the body of the while
loop on that.
Martien
--
|
Martien Verbruggen | Blessed are the Fundamentalists, for they
| shall inhibit the earth.
|
------------------------------
Date: Wed, 05 Mar 2008 22:46:00 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Parsing blocks of text in Perl
Message-Id: <638ip9F26eoklU1@mid.individual.net>
Martien Verbruggen wrote:
> Next time, before you post here, show us what you have tried first.
That's good advice.
> This is not a place where you can coe to get free code all the time,
Isn't it? You just made me believe it is. ;-)
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Wed, 5 Mar 2008 14:16:53 -0800 (PST)
From: mxyzplk <ernest.mueller@ni.com>
Subject: Re: Parsing blocks of text in Perl
Message-Id: <560fcc8c-d5b5-4f62-af30-286806a00d83@8g2000hse.googlegroups.com>
Thanks man. Here's what I was trying to do, without splitting:
#!/bin/perl
#
# Usage: linkxfer.pl file
#
((($file) = @ARGV) == 1 && -f $file)
|| die "Usage: $0 file\n";
open(IN,"$file");
$|=1;
while ($line=<IN>) {
$line =~ s/\[\[([^:]+?)\|(.+?\]\])/[[General:\1|\2/g;
print $line;
}
close IN;
exit;
It was working for the adding "General:" part, and I was trying to
figure out how the heck to apply "tr" to the \1 in the output and came
to a standstill. Apparently it's the magic /e flag plus a subroutine
to the rescue; using your example I did:
#!/bin/perl
#
# Usage: linkxfer.pl file
#
((($file) = @ARGV) == 1 && -f $file)
|| die "Usage: $0 file\n";
open(IN,"$file");
$|=1;
while ($line=<IN>) {
$line =~ s/\[\[([^:]+?)\|(.+?\]\])/[[General:\1|$2/g;
$line =~ s/\[\[([^:]+?):(.+?\|.+?\]\])/'[[' . transform($1) . ":$2"/
ge;
print $line;
}
close IN;
sub transform
{
my $string = shift;
$string =~ tr/[^a-zA-Z0-9]//cd;
return $string;
}
exit;
Although I do think your version's more elegant and extensible.
Thanks,
Ernest
------------------------------
Date: Wed, 5 Mar 2008 14:23:53 -0800 (PST)
From: mxyzplk <ernest.mueller@ni.com>
Subject: Re: Parsing blocks of text in Perl
Message-Id: <665eb9b1-6482-45ae-947e-67ab02f1c4d0@34g2000hsz.googlegroups.com>
Sorry to come across as a code mooch, I was more looking for a
direction to go with it than finished code, because I wasn't at all
sure about my general approach and whether I should be instead doing
something fancier with Text::Balanced or some other parser... Thanks
to Martien and hugs to all the grouchy Europeans out there!
------------------------------
Date: Wed, 05 Mar 2008 16:40:53 -0000
From: Chris Mattern <syscjm@sumire.gwu.edu>
Subject: Re: Secure FTP
Message-Id: <slrnfstj8l.ga2.syscjm@sumire.gwu.edu>
On 2008-02-29, john_paul_byrne@yahoo.co.uk <john_paul_byrne@yahoo.co.uk> wrote:
> Hi,
>
> I am setting up a cron job which calls a perl script, at the end of
> which, I want to upload a file to a secure ftp server.
>
> Can somebody point me in the best way to approach this? A sample would
> be fantastic ;o)
>
> John
http://search.cpan.org/search?query=sftp&mode=all
--
Christopher Mattern
NOTICE
Thank you for noticing this new notice
Your noticing it has been noted
And will be reported to the authorities
------------------------------
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 V11 Issue 1335
***************************************