How do you use path handling (File::Spec, Path::Tiny) with a short example?

Perl provides several modules for handling file paths, two of the most popular being File::Spec and Path::Tiny. These modules facilitate working with file system paths in a way that is both platform-independent and convenient.

Keywords: Perl, File::Spec, Path::Tiny, path handling, file system
Description: Learn how to use Perl's File::Spec and Path::Tiny for effective path handling in your applications.

Here’s a short example demonstrating both modules:

# Using File::Spec to create a path use File::Spec; my $dir = 'example'; my $file = 'test.txt'; my $path = File::Spec->catfile($dir, $file); print "File path using File::Spec: $path\n"; # Using Path::Tiny for file manipulation use Path::Tiny; my $file_path = path($path); $file_path->spew('Hello World!'); # Write to file my $content = $file_path->slurp; # Read from file print "File content: $content\n";

Keywords: Perl File::Spec Path::Tiny path handling file system