Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Seekers of Perl Wisdom

( #479=superdoc: print w/ replies, xml ) Need Help??

If you have a question on how to do something in Perl, or you need a Perl solution to an actual real-life problem, or you're unsure why something you've tried just isn't working... then this section is the place to ask. Post a new question!

However, you might consider asking in the chatterbox first (if you're a registered user). The response time tends to be quicker, and if it turns out that the problem/solutions are too much for the cb to handle, the kind monks will be sure to direct you here.

User Questions
Find and Replace with special characters
2 direct replies — Read more / Contribute
by perl2newbie
on May 06, 2015 at 20:07

    Hi guys, im trying to replace #<-(0N<-(s3T with *<-(0N<-(s3T

    so im using: ...... foreach(@lines) { $_=~ $_ =~ s/\#<-(0N<-(s3T\/\*<-(0N<-(s3T/g; push(@newlines,$_); } ...
    i get an error. 'unmatched < in regex......' Can you please help. Thanks
HTML Template
2 direct replies — Read more / Contribute
by raptorsoul
on May 06, 2015 at 20:03

    Okay, here's my quandary. Our church website was written by someone who has now moved away. I have taken over the site, but know very little about perl. The site ran fine until recently when I updated the server from XP to Windows 7. Now running the original perlscript gives a 502 error. The perlscript in question was designed to search the contents of a specified folder and find all of the .wmv files (recorded sermons). It then creates an HTML form from a template, providing links to these files. It also listed the date and speaker for each file, which is determined by the filename. I have tried very simple scripts and they have all worked fine, but the original script does not. I think that the problem may reside in the upgrade from 32 bit to 64 bit, or possibly the upgrade in ActivePearl. Here is the code where I suspect the problem lies. Please keep in mind, I didn't write this! LOL!

    #!d:/perl/bin/perl -w ######!/usr/bin/perl -w $yeardir=$ENV{'QUERY_STRING'}; use HTMLTMPL; # Create the template object and load it. $templ = new HTMLTMPL; $templ->src('d:/wwwroot/calvarybaptist/cgi-bin/sermon/sermons.html'); #get the list of audio files from the sermons directory #for this to work correctly the files should be names xxxxxxsP.ram # replace the xxxxxx with the date ie 100399 # replace the P with G for Pastor Gerhardt or V for Guest Speaker # replace the s with M for morning sermon, e for evening sermon or w f +or midweek # ex 100399MG would be the sermon for October 3, 1999 Morning's Sermon + by Pastor Gerhardt # if there is a technical problem with the sermon add a t or T to the +end ie # ex 100399MGt #opendir(DIR, "/webpages/calvarybaptist/sermons/"); #opendir(DIR, "/home/rmserver/Content/"); #opendir(DIR, "d:/Sermons/"); opendir(DIR, "d:/wwwroot/CalvaryBaptist/Sermons/".$yeardir); @ramFiles = sort(grep(/\.wmv$/,readdir(DIR))); #@ramFiles = sort(grep(/\.rm$/,readdir(DIR))); #@ramFiles = sort(grep(/\.ram$/,readdir(DIR))); #@ramFiles = sort(grep(/\.HTML$/,readdir(DIR))); closedir(DIR); foreach $i (@ramFiles) { #MMDDYYSD #MM = Month 0-1 (2) #DD = Day 2-3 (2) #YY = Year 4-5 (2) #S = Sermon (1) # M = Morning # e = Evening # w = Wednesday # s = Sunday School # x = Special #D = Who delivered # G = Gerhardt # H = Halverson # B = Brown # V = Guest # N = None $month=substr($i,0,2); $day=substr($i,2,2); $year=substr($i,4,2); if ((substr($i,6,1) eq "m") or (substr($i,6,1) eq "M")){ $whichsermon="Morning Sermon"; } elsif ((substr($i,6,1) eq "e") or (substr($i,6,1) eq "E")){ $whichsermon="Evening Sermon"; } elsif ((substr($i,6,1) eq "w") or (substr($i,6,1) eq "W")){ $whichsermon="Midweek Sermon"; } elsif ((substr($i,6,1) eq "s") or (substr($i,6,1) eq "S")){ $whichsermon="Sunday School"; } elsif ((substr($i,6,1) eq "x") or (substr($i,6,1) eq "X")){ $whichsermon="Special"; } else { $whichsermon="ERROR Determing Sermon."; } # Determin who delivered the sermon #if ((substr($i,7,1) eq "g") or (substr($i,7,1) eq "G")){ # $speaker="Pastor Gerhardt"; #} #elsif ((substr($i,7,1) eq "b") or (substr($i,7,1) eq "B")){ # $speaker="Asst. Pastor Brown"; #} #elsif ((substr($i,7,1) eq "h") or (substr($i,7,1) eq "H")){ # $speaker="Pastor Halverson"; #} #elsif ((substr($i,7,1) eq "v") or (substr($i,7,1) eq "V")){ # $speaker="Guest Speaker"; #} #elsif ((substr($i,7,1) eq "n") or (substr($i,7,1) eq "N")){ # $speaker=""; #} #else { # $speaker="ERROR code for speaker is incorrect." #} #Check to see of there was a technical problem with the sermon. # if ((substr($i,8,1) eq "T") or (substr($i,8,1) eq "t")){ # $speaker="Technical Problems"; # } $desc=substr($i,8,length($i)-12); #.' '.(length($i)).' '.$i.'!'; $filename=$yeardir."/".$i; $test=$month.'/'.$day.'/'.$year.' '.$whichsermon.', '.$desc.' '; # $templ->filename("$i", 'x_details'); $templ->filename("$filename", 'x_details'); $templ->desc("$test", 'x_details'); } # Send the completed html document to the web server. $templ->output('Content-Type: text/html');
    Here is the script that I suspect is in question.
Call to subs in a Dispatch Table
1 direct reply — Read more / Contribute
by Anonymous Monk
on May 06, 2015 at 15:35
    Hi Monks!

    I am trying to call two subs at the same time at this particular time once I call this value "results" in my dispatch table code.
    Is it possible and how can it be done? This is not working!

    ... my %general = ( data => sub { info( who=> $q->param('who') || '', why=> $q->param('why') || '', ) }, results => sub { names( first => $q->param('first'), zip => $q->param('zip'), ) }, sub { info( who => $q->param('who') || '', why => $q->param('why') || '', ) }, ); ...

    Thank for looking!
Sub Return Help
1 direct reply — Read more / Contribute
by PerlCramps
on May 06, 2015 at 15:09
    I am new to perl; But I am trying. I have a text file name hosts.txt with two lines hostone and hosttwo. I have created a sub sub get_host { open( HOST, "<hosts.txt"); while(<HOST>) { my($line) = $_; chomp($line); print "$line\n"; return($_); } } When I call the sub my $host = get_host(); print "hostname = $host"; I am getting the following output hostone hostname = hostone I want to be able get the output for each line entry. any other help would be appreciated !!! Thanks
Calling subroutine with a scalar
1 direct reply — Read more / Contribute
by Rodster001
on May 06, 2015 at 14:07
    Hi Monks, I know this has been asked endlessly, but I am doing it slightly different than I have done before and I cannot quite get the syntax right.
    my $res = new Some::Package({ foo => bar })->subroutine;
    What I want to do is call "subroutine" by variable. For example:
    my $sub = "subroutine"; my $res = new Some::Package({ foo => bar })->( $sub );
    That doesn't work, I've tried several variations of that (along with \&) and I can't quite get it. Can someone enlighten me?

Replacement for Python's flask and Apple's Bonjour
2 direct replies — Read more / Contribute
by sg
on May 06, 2015 at 13:17
search contents of one file and replace with contents of another to make a new file
5 direct replies — Read more / Contribute
by vcorby
on May 06, 2015 at 12:43

    Dearest Monks,

    I am trying to replace the text in file #1 with the text in file #2 to make file #3. I would like to do so based on the identity between the text in file #1 and the text before the underscore (_) in file #2 to make file #3. The basic idea is to be able to add the content after the underscore (which is indicated in file #2) into each entry of file #1, but I want to maintain the commas and line breaks found in file #1 so that it can be read into another program.

    Here are smaller versions of my files:

    File 1 (1.txt):


    File 2 (2.txt):


    I want to make File 3 (3.txt):


    Based on a similar post, I thought I could do the following (please excuse my child-like annotations):

    #!/usr/local/bin/perl use warnings; use strict; my $f1 = '1.txt'; # set up files 1 through 3 my $f2 = '2.txt'; my $f3 = '3.txt'; my (@strings, $text); open (FH, $f1) || die; # open file 1 in a file handle chomp(@strings = <FH>); # take off the line breaks open (FH, $f2) || die; # open file 2 in a file handle $text = join '', <FH>; # joins files 1 and 2? - I don't think I wa +nt to do this... $text =~ s/\Q$_\E/$f3/g for @strings; # this is the problem spot, +I think... open (FH, ">$f3") || die; print FH $text; close FH;

    but obviously not because when I do I get this for 3.txt:


    Any ideas?

    And many thanks in advance.


Tracking users clicks throughout an application
1 direct reply — Read more / Contribute
by Anonymous Monk
on May 06, 2015 at 11:15
    Hi Monks!

    I need to track actions by users in my web page, like where they clicked, pages they visited and store all this to a database.
    I looked into CGI::Session, but I am wondering if some of you had done something like in Perl and could pointing me to the right direction.

    Thanks for the help!
alarm and reading from file decriptor
1 direct reply — Read more / Contribute
by tomtastic
on May 06, 2015 at 10:44
    If I trigger an alarm, subsequent reading from a filedescriptor seems broken in an odd way :
    $ ./ 1
    #MAIN, a
    #MAIN, b
    #MAIN, c
    Code works as expected when alarm isn't triggered :
    $ ./ 10
    #MAIN, a
    #MAIN, b
    #MAIN, c
    #MAIN, a
    #MAIN, b
    #MAIN, c
    #MAIN, a
    #MAIN, b
    #MAIN, c
    Note: The code is an example only, I'm unable to move the inline '#MAIN' tags to a __DATA__ section in my legacy codebase. So perhaps this is a quirk of opening a filehandle to the script itself?
    #!/usr/bin/env perl # Example code to show how reading from file descriptors fails after a +n alarm is triggered? use strict; use warnings; my $timeout = defined($ARGV[0]) ? $ARGV[0] : 2; my $sleep = 5; print "TIMEOUT=$timeout\n"; print "SLEEP=$sleep\n\n"; #MAIN, a #MAIN, b #MAIN, c sub sub1 { open FD1, "$0" || die "ERROR: Could not open $0 : $!\n"; $. = defined($.) ? $. : 0; print "(sub1):begin_pos:$.\n"; while (<FD1>){ next if ! /^#MAIN|^#DATA/; print $_; } eval { local $SIG{ALRM} = sub { die "alarm\n"; }; alarm $timeout; print "(sub1):sleeping...\n"; my $cmd = `sleep $sleep 2>&1;echo slept`; alarm 0; }; if ($@){ print "(sub1):timed_out!\n"; } else { # did not time out } print "(sub1):end_pos:$.\n\n"; close FD1 or die "$!\n"; } sub sub2 { open FD2, "$0" || die "ERROR: Could not open $0 : $!\n"; print "(sub2):begin_pos:$.\n"; while (<FD2>){ next if ! /^#MAIN|^#DATA/; print $_; } print "(sub2):end_pos:$.\n\n"; close FD2 or die "$!\n"; } &sub1; &sub2; &sub2;
Module caller
2 direct replies — Read more / Contribute
by jeffsto
on May 06, 2015 at 09:29
    I've started putting my code into modules for obvious reasons. Is there a way I can get the name of the script that is calling the module? I want to use this for tracking so I can easily keep track of what scripts call my modules. I've looked at the module "caller" however I couldn't determine if it could extract the correct information. It seemed like it was limited to the information within the module itself, or did I miss something? Thanks!

Add your question
Your question:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (5)
    As of 2015-05-07 05:22 GMT
    Find Nodes?
      Voting Booth?

      In my home, the TV remote control is ...

      Results (155 votes), past polls