Reading a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Procor

    Reading a file

    Hello,

    i'm new with perl.

    I want to read a file and display this when visiting my website :

    In my browser : http://www.website.nl/displayfile.pl
    =============== ====
    #!/usr/local/bin/perl
    $logurl = "/opt/guide/www.website.nl/log.txt";
    # Begin the Editing of log File
    print $logurl;
    =============== ======
    It doesn't work!!

    Why?

    Marco




  • Jim Gibson

    #2
    Re: Reading a file

    In article <432f261b$0$143 82$ba620dc5@tex t.nova.planet.n l>, Procor
    <procornonews@n ewsprocor.news> wrote:
    [color=blue]
    > Hello,
    >
    > i'm new with perl.
    >
    > I want to read a file and display this when visiting my website :
    >
    > In my browser : http://www.website.nl/displayfile.pl
    > =============== ====
    > #!/usr/local/bin/perl
    > $logurl = "/opt/guide/www.website.nl/log.txt";
    > # Begin the Editing of log File
    > print $logurl;
    > =============== ======
    > It doesn't work!!
    >
    > Why?[/color]

    How does it not work?

    You have to generate a complete HTTP response message, including the
    appropriate headers. The easiest way to do that in Perl is to use the
    CGI module and its header, start_html, and end_html subroutines.
    Generating proper HTML for HTTP can be a complicated task, so you have
    some studying to do. Start with 'perldoc CGI' for examples.

    If you want to display the contents of a file, rather than just its
    path name, you will have to open the file, read it, and print its lines
    to the output. Look at Perl's open, print, and close functions, and the
    'perldoc perlio' section of your online documentation.

    FYI: the comp.lang.perl newsgroup is defunct. Try comp.lang.perl. misc
    in the future or just stick with alt.perl.

    Posted Via Usenet.com Premium Usenet Newsgroup Services
    ----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    ----------------------------------------------------------
    Best Usenet Service Providers 2025 ranked by Newsgroup Access Newsservers, Usenet Search, Features & Free Trial. Add VPN for privacy.

    Comment

    • Jürgen Exner

      #3
      Re: Reading a file

      Procor wrote:[color=blue]
      > Hello,
      >
      > i'm new with perl.
      >
      > I want to read a file and display this when visiting my website :
      >
      > In my browser : http://www.website.nl/displayfile.pl
      > =============== ====
      > #!/usr/local/bin/perl
      > $logurl = "/opt/guide/www.website.nl/log.txt";
      > # Begin the Editing of log File
      > print $logurl;
      > =============== ======
      > It doesn't work!![/color]

      Hmmm, strange. By all means your program should print the verbatim text

      /opt/guide/www.website.nl/log.txt

      to STDOUT, usually your terminal.
      And it does when I run your snippet on my computer.

      C:\tmp>t.pl
      /opt/guide/www.website.nl/log.txt
      C:\tmp>

      What do you expect the code snippet to do instead?
      [color=blue]
      > Why?[/color]

      Unless you tell us what you expect this code snippet to do we cannot tell
      what you did wrong because we don't know your goal.

      jue


      Comment

      • Bill

        #4
        Re: Reading a file


        "Procor" <procornonews@n ewsprocor.news> wrote in message
        news:432f261b$0 $14382$ba620dc5 @text.nova.plan et.nl...[color=blue]
        > Hello,
        >
        > i'm new with perl.
        >
        > I want to read a file and display this when visiting my website :
        >
        > In my browser : http://www.website.nl/displayfile.pl
        > =============== ====
        > #!/usr/local/bin/perl
        > $logurl = "/opt/guide/www.website.nl/log.txt";
        > # Begin the Editing of log File
        > print $logurl;
        > =============== ======
        > It doesn't work!!
        >
        > Why?
        >
        > Marco
        >
        >
        >
        >[/color]

        # added -w (warn) I think you will need it
        #!/usr/local/bin/perl -w

        # ok you defined what file you want to view
        $logurl = "/opt/guide/www.website.nl/log.txt";

        # this actually opens the file for use and returns any errors
        open (FILE, "$logurl") or die "Couldn't open $logurl: $!";.

        # you will need to call each line of the file for use.
        # $_ is what you will be working with.
        # With each pass through the loop $_ will have the new line data.
        while (<FILE>) {
        print $_; # what you do with each line in here is up to you.
        }

        close (FILE); # be nice and close the file

        Hope that helps,
        Bill


        Comment

        Working...