SlideShare a Scribd company logo
Perl Brown Bag


            File Handles



              Shaun Griffith
               May 1, 2006

05/13/12        ATI Confidential   1
Agenda
Filehandles
   • What are they?
     • Predefined
     • Open
     • Close
     • EOF
     • Pipes
FTP Scripting
     •Non-Perl FTP hacking



05/13/12                       2
File Handles
What are File Handles?
     • File handles act like a reference to a
     specific entity in the file system.
     • Ordinarily associated with a file or a
     stream.
         • A “stream” is data piped through a
           process (e.g., cat or gzip).




05/13/12                                        3
How do You Call Them?
How do you reference them?
     • File handles do not have a sigil (i.e.,
     no $/@/%)
     • Just a bare identifier, by convention,
     all uppercase:
         • MY_FILE_HANDLE
         • XYZ
         • ABC




05/13/12                                         4
Predefined File Handles
Perl has several predefined file
handles:
• Like “C”:
   • STDIN
   • STDOUT (default for print)
   • STDERR (default for warn/die)
• Unique to Perl:
   • ARGV (default for <>)

     • DATA
           • Perl “magic”
                                                ARGV
        • inline after __DATA__                   !
               • (note the double underlines)



05/13/12                                               5
Open and Close
Opening a file:
• open(STDIN,‘<‘, “myfile.txt”)
•   or die “Error opening
myfile.txt for reading, $! “;
     • $! holds the error message.
     • Use parens for open to avoid problems with
     expressions as parameters.
Closing a file:
•close STDIN;
   • or die only needed in rare cases for close
2 Argument open
• Older Perls
• Less safe
     • open(STDIN, “< $somefile”) or die…
     •$somefile could be “x;rm –r /*”
05/13/12                                            6
EOF
Why check for EOF?
• while (<>) does this for you…
• But with multiple input files, $. (input line number) is not
reset.
• To reset the line number for each new file:
    • while (<>)
     • { do something here }
     • continue
     • { close( ARGV ) if eof }
•Note: no parens!!!
    • eof is the same as eof( ARGV ), the current file read
     with <>
     • eof() is the final file in @ARGV
•Other uses – output filename derived from input filename:
    • if ( eof )
     •{ close( ARGV );
     • open(STDOUT,‘>’,$ARGV[0].”out”; }
05/13/12                                                         7
Pipes
What are pipes?
• Chain processes together to work on a data stream
• Example:
    • noblanks *.txt | fix_names | mail joe@x.com
In Perl?
• open(STDIN,‘<‘,‘gzip            -d myfile.gz |’) or die…
    •This unzips the file directly into Perl.
    •No other special handling required.
         • while (<STDIN>) { do something here }
• open(STDOUT,’>’,’| gzip –c >myfile.gz’) or die…
    •This zips the file directly from Perl.
    •No other special handling required.
         • print “You’re data heren”;
•Some useful Unix commands:
    •mail
    •gzip
    •tee

05/13/12                                                     8
FTP Hacking
FTP also takes input from a control file
       ftp –n –i < control_file
         -n: don’t automate login (easier to script)
         -i: don’t prompt for mget
What does the control file look like?
    open ftp.urcompany.com
    user myuser mypassword
    cd somedir/otherdir
    bin
    get somefile.lotgdf
    mget lots-o-files*.lotgdf
    quit
Notes:
    Must already know directory tree & files
    bin for binary (sometimes type i or image)
    get, put, mget, mput
    Don’t forget to quit

05/13/12                                               9
Next Time?
Command Line Arguments?
     •Catching them
     •Checking them
     •Using them
     •My pet module




05/13/12                           10

More Related Content

What's hot

Python internals and how they affect your code - kasra ahmadvand
Python internals and how they affect your code - kasra ahmadvandPython internals and how they affect your code - kasra ahmadvand
Python internals and how they affect your code - kasra ahmadvand
irpycon
 
Php extensions
Php extensionsPhp extensions
Php extensions
Elizabeth Smith
 
Introduction to Programming in Go
Introduction to Programming in GoIntroduction to Programming in Go
Introduction to Programming in Go
Amr Hassan
 
Hacking with hhvm
Hacking with hhvmHacking with hhvm
Hacking with hhvm
Elizabeth Smith
 
Yapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed PerlYapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed Perl
Hideaki Ohno
 
Php7 hhvm and co
Php7 hhvm and coPhp7 hhvm and co
Php7 hhvm and co
Pierre Joye
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 Verona
Patrick Allaert
 
Php Extensions for Dummies
Php Extensions for DummiesPhp Extensions for Dummies
Php Extensions for Dummies
Elizabeth Smith
 
Happy Go Programming
Happy Go ProgrammingHappy Go Programming
Happy Go Programming
Lin Yo-An
 
F# Type Provider for R Statistical Platform
F# Type Provider for R Statistical PlatformF# Type Provider for R Statistical Platform
F# Type Provider for R Statistical Platform
Howard Mansell
 
Apache Avro in LivePerson [Hebrew]
Apache Avro in LivePerson [Hebrew]Apache Avro in LivePerson [Hebrew]
Apache Avro in LivePerson [Hebrew]
LivePerson
 
Introduction to the rust programming language
Introduction to the rust programming languageIntroduction to the rust programming language
Introduction to the rust programming language
Nikolay Denev
 
Code quality tools for dev
Code quality tools for devCode quality tools for dev
Code quality tools for dev
Deepu S Nath
 
Apache AVRO (Boston HUG, Jan 19, 2010)
Apache AVRO (Boston HUG, Jan 19, 2010)Apache AVRO (Boston HUG, Jan 19, 2010)
Apache AVRO (Boston HUG, Jan 19, 2010)
Cloudera, Inc.
 
Github.com anton terekhov-orientdb-php
Github.com anton terekhov-orientdb-phpGithub.com anton terekhov-orientdb-php
Github.com anton terekhov-orientdb-php
San jay
 
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, ItalyPHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
Patrick Allaert
 
JavaOne 2016 - JVM assisted sensitive data
JavaOne 2016 - JVM assisted sensitive dataJavaOne 2016 - JVM assisted sensitive data
JavaOne 2016 - JVM assisted sensitive data
Charlie Gracie
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"
Fwdays
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHP
BUDNET
 
Functions in python
Functions in pythonFunctions in python
Functions in python
Santosh Verma
 

What's hot (20)

Python internals and how they affect your code - kasra ahmadvand
Python internals and how they affect your code - kasra ahmadvandPython internals and how they affect your code - kasra ahmadvand
Python internals and how they affect your code - kasra ahmadvand
 
Php extensions
Php extensionsPhp extensions
Php extensions
 
Introduction to Programming in Go
Introduction to Programming in GoIntroduction to Programming in Go
Introduction to Programming in Go
 
Hacking with hhvm
Hacking with hhvmHacking with hhvm
Hacking with hhvm
 
Yapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed PerlYapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed Perl
 
Php7 hhvm and co
Php7 hhvm and coPhp7 hhvm and co
Php7 hhvm and co
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 Verona
 
Php Extensions for Dummies
Php Extensions for DummiesPhp Extensions for Dummies
Php Extensions for Dummies
 
Happy Go Programming
Happy Go ProgrammingHappy Go Programming
Happy Go Programming
 
F# Type Provider for R Statistical Platform
F# Type Provider for R Statistical PlatformF# Type Provider for R Statistical Platform
F# Type Provider for R Statistical Platform
 
Apache Avro in LivePerson [Hebrew]
Apache Avro in LivePerson [Hebrew]Apache Avro in LivePerson [Hebrew]
Apache Avro in LivePerson [Hebrew]
 
Introduction to the rust programming language
Introduction to the rust programming languageIntroduction to the rust programming language
Introduction to the rust programming language
 
Code quality tools for dev
Code quality tools for devCode quality tools for dev
Code quality tools for dev
 
Apache AVRO (Boston HUG, Jan 19, 2010)
Apache AVRO (Boston HUG, Jan 19, 2010)Apache AVRO (Boston HUG, Jan 19, 2010)
Apache AVRO (Boston HUG, Jan 19, 2010)
 
Github.com anton terekhov-orientdb-php
Github.com anton terekhov-orientdb-phpGithub.com anton terekhov-orientdb-php
Github.com anton terekhov-orientdb-php
 
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, ItalyPHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
 
JavaOne 2016 - JVM assisted sensitive data
JavaOne 2016 - JVM assisted sensitive dataJavaOne 2016 - JVM assisted sensitive data
JavaOne 2016 - JVM assisted sensitive data
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHP
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 

Viewers also liked

Perl Intro 6 Ftp
Perl Intro 6 FtpPerl Intro 6 Ftp
Perl Intro 6 Ftp
Shaun Griffith
 
Perl Intro 2 First Program
Perl Intro 2 First ProgramPerl Intro 2 First Program
Perl Intro 2 First Program
Shaun Griffith
 
Perl Intro 7 Subroutines
Perl Intro 7 SubroutinesPerl Intro 7 Subroutines
Perl Intro 7 Subroutines
Shaun Griffith
 
Perl Intro 4 Debugger
Perl Intro 4 DebuggerPerl Intro 4 Debugger
Perl Intro 4 Debugger
Shaun Griffith
 
Perl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And SubstitutionsPerl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And Substitutions
Shaun Griffith
 
Perl Intro 3 Datalog Parsing
Perl Intro 3 Datalog ParsingPerl Intro 3 Datalog Parsing
Perl Intro 3 Datalog Parsing
Shaun Griffith
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving Cars
LinkedIn
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI Explainer
Luminary Labs
 

Viewers also liked (8)

Perl Intro 6 Ftp
Perl Intro 6 FtpPerl Intro 6 Ftp
Perl Intro 6 Ftp
 
Perl Intro 2 First Program
Perl Intro 2 First ProgramPerl Intro 2 First Program
Perl Intro 2 First Program
 
Perl Intro 7 Subroutines
Perl Intro 7 SubroutinesPerl Intro 7 Subroutines
Perl Intro 7 Subroutines
 
Perl Intro 4 Debugger
Perl Intro 4 DebuggerPerl Intro 4 Debugger
Perl Intro 4 Debugger
 
Perl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And SubstitutionsPerl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And Substitutions
 
Perl Intro 3 Datalog Parsing
Perl Intro 3 Datalog ParsingPerl Intro 3 Datalog Parsing
Perl Intro 3 Datalog Parsing
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving Cars
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI Explainer
 

Similar to Perl Intro 8 File Handles

System Programming and Administration
System Programming and AdministrationSystem Programming and Administration
System Programming and Administration
Krasimir Berov (Красимир Беров)
 
Data file handling
Data file handlingData file handling
Data file handling
Saurabh Patel
 
Data file handling
Data file handlingData file handling
Data file handling
TAlha MAlik
 
PerlScripting
PerlScriptingPerlScripting
PerlScripting
Aureliano Bombarely
 
44CON London 2015 - 15-Minute Linux Incident Response Live Analysis
44CON London 2015 - 15-Minute Linux Incident Response Live Analysis44CON London 2015 - 15-Minute Linux Incident Response Live Analysis
44CON London 2015 - 15-Minute Linux Incident Response Live Analysis
44CON
 
[HES2013] Virtually secure, analysis to remote root 0day on an industry leadi...
[HES2013] Virtually secure, analysis to remote root 0day on an industry leadi...[HES2013] Virtually secure, analysis to remote root 0day on an industry leadi...
[HES2013] Virtually secure, analysis to remote root 0day on an industry leadi...
Hackito Ergo Sum
 
File io
File ioFile io
File io
Pri Dhaka
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Files
primeteacher32
 
Bioinformatics p4-io v2013-wim_vancriekinge
Bioinformatics p4-io v2013-wim_vancriekingeBioinformatics p4-io v2013-wim_vancriekinge
Bioinformatics p4-io v2013-wim_vancriekinge
Prof. Wim Van Criekinge
 
Files in php
Files in phpFiles in php
Files in php
sana mateen
 
03-01-File Handling python.pptx
03-01-File Handling python.pptx03-01-File Handling python.pptx
03-01-File Handling python.pptx
qover
 
Go for SysAdmins - LISA 2015
Go for SysAdmins - LISA 2015Go for SysAdmins - LISA 2015
Go for SysAdmins - LISA 2015
Chris McEniry
 
Static and Dynamic Analysis at Ning
Static and Dynamic Analysis at NingStatic and Dynamic Analysis at Ning
Static and Dynamic Analysis at Ning
ZendCon
 
Surviving OS X as a Windows Admin
Surviving OS X as a Windows AdminSurviving OS X as a Windows Admin
Surviving OS X as a Windows Admin
Dell World
 
Perl for System Automation - 01 Advanced File Processing
Perl for System Automation - 01 Advanced File ProcessingPerl for System Automation - 01 Advanced File Processing
Perl for System Automation - 01 Advanced File Processing
Danairat Thanabodithammachari
 
php fundamental
php fundamentalphp fundamental
php fundamental
zalatarunk
 
php
phpphp
Php introduction with history of php
Php introduction with history of phpPhp introduction with history of php
Php introduction with history of php
pooja bhandari
 
Automatic tool for static analysis
Automatic tool for static analysisAutomatic tool for static analysis
Automatic tool for static analysis
Chong-Kuan Chen
 
Master perl io_2011
Master perl io_2011Master perl io_2011
Master perl io_2011
Paolo Marcatili
 

Similar to Perl Intro 8 File Handles (20)

System Programming and Administration
System Programming and AdministrationSystem Programming and Administration
System Programming and Administration
 
Data file handling
Data file handlingData file handling
Data file handling
 
Data file handling
Data file handlingData file handling
Data file handling
 
PerlScripting
PerlScriptingPerlScripting
PerlScripting
 
44CON London 2015 - 15-Minute Linux Incident Response Live Analysis
44CON London 2015 - 15-Minute Linux Incident Response Live Analysis44CON London 2015 - 15-Minute Linux Incident Response Live Analysis
44CON London 2015 - 15-Minute Linux Incident Response Live Analysis
 
[HES2013] Virtually secure, analysis to remote root 0day on an industry leadi...
[HES2013] Virtually secure, analysis to remote root 0day on an industry leadi...[HES2013] Virtually secure, analysis to remote root 0day on an industry leadi...
[HES2013] Virtually secure, analysis to remote root 0day on an industry leadi...
 
File io
File ioFile io
File io
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Files
 
Bioinformatics p4-io v2013-wim_vancriekinge
Bioinformatics p4-io v2013-wim_vancriekingeBioinformatics p4-io v2013-wim_vancriekinge
Bioinformatics p4-io v2013-wim_vancriekinge
 
Files in php
Files in phpFiles in php
Files in php
 
03-01-File Handling python.pptx
03-01-File Handling python.pptx03-01-File Handling python.pptx
03-01-File Handling python.pptx
 
Go for SysAdmins - LISA 2015
Go for SysAdmins - LISA 2015Go for SysAdmins - LISA 2015
Go for SysAdmins - LISA 2015
 
Static and Dynamic Analysis at Ning
Static and Dynamic Analysis at NingStatic and Dynamic Analysis at Ning
Static and Dynamic Analysis at Ning
 
Surviving OS X as a Windows Admin
Surviving OS X as a Windows AdminSurviving OS X as a Windows Admin
Surviving OS X as a Windows Admin
 
Perl for System Automation - 01 Advanced File Processing
Perl for System Automation - 01 Advanced File ProcessingPerl for System Automation - 01 Advanced File Processing
Perl for System Automation - 01 Advanced File Processing
 
php fundamental
php fundamentalphp fundamental
php fundamental
 
php
phpphp
php
 
Php introduction with history of php
Php introduction with history of phpPhp introduction with history of php
Php introduction with history of php
 
Automatic tool for static analysis
Automatic tool for static analysisAutomatic tool for static analysis
Automatic tool for static analysis
 
Master perl io_2011
Master perl io_2011Master perl io_2011
Master perl io_2011
 

Perl Intro 8 File Handles

  • 1. Perl Brown Bag File Handles Shaun Griffith May 1, 2006 05/13/12 ATI Confidential 1
  • 2. Agenda Filehandles • What are they? • Predefined • Open • Close • EOF • Pipes FTP Scripting •Non-Perl FTP hacking 05/13/12 2
  • 3. File Handles What are File Handles? • File handles act like a reference to a specific entity in the file system. • Ordinarily associated with a file or a stream. • A “stream” is data piped through a process (e.g., cat or gzip). 05/13/12 3
  • 4. How do You Call Them? How do you reference them? • File handles do not have a sigil (i.e., no $/@/%) • Just a bare identifier, by convention, all uppercase: • MY_FILE_HANDLE • XYZ • ABC 05/13/12 4
  • 5. Predefined File Handles Perl has several predefined file handles: • Like “C”: • STDIN • STDOUT (default for print) • STDERR (default for warn/die) • Unique to Perl: • ARGV (default for <>) • DATA • Perl “magic” ARGV • inline after __DATA__ ! • (note the double underlines) 05/13/12 5
  • 6. Open and Close Opening a file: • open(STDIN,‘<‘, “myfile.txt”) • or die “Error opening myfile.txt for reading, $! “; • $! holds the error message. • Use parens for open to avoid problems with expressions as parameters. Closing a file: •close STDIN; • or die only needed in rare cases for close 2 Argument open • Older Perls • Less safe • open(STDIN, “< $somefile”) or die… •$somefile could be “x;rm –r /*” 05/13/12 6
  • 7. EOF Why check for EOF? • while (<>) does this for you… • But with multiple input files, $. (input line number) is not reset. • To reset the line number for each new file: • while (<>) • { do something here } • continue • { close( ARGV ) if eof } •Note: no parens!!! • eof is the same as eof( ARGV ), the current file read with <> • eof() is the final file in @ARGV •Other uses – output filename derived from input filename: • if ( eof ) •{ close( ARGV ); • open(STDOUT,‘>’,$ARGV[0].”out”; } 05/13/12 7
  • 8. Pipes What are pipes? • Chain processes together to work on a data stream • Example: • noblanks *.txt | fix_names | mail joe@x.com In Perl? • open(STDIN,‘<‘,‘gzip -d myfile.gz |’) or die… •This unzips the file directly into Perl. •No other special handling required. • while (<STDIN>) { do something here } • open(STDOUT,’>’,’| gzip –c >myfile.gz’) or die… •This zips the file directly from Perl. •No other special handling required. • print “You’re data heren”; •Some useful Unix commands: •mail •gzip •tee 05/13/12 8
  • 9. FTP Hacking FTP also takes input from a control file ftp –n –i < control_file -n: don’t automate login (easier to script) -i: don’t prompt for mget What does the control file look like? open ftp.urcompany.com user myuser mypassword cd somedir/otherdir bin get somefile.lotgdf mget lots-o-files*.lotgdf quit Notes: Must already know directory tree & files bin for binary (sometimes type i or image) get, put, mget, mput Don’t forget to quit 05/13/12 9
  • 10. Next Time? Command Line Arguments? •Catching them •Checking them •Using them •My pet module 05/13/12 10